View Full Version : [Tutorial] Creating A Basic Speedometer

02/11/2010, 03:48 AM
My comments explain it pretty good. Please read this and do not just copy and paste it.
#include <a_samp>

new Float:svx[MAX_PLAYERS]; // Holds Velocity X
new Float:svy[MAX_PLAYERS]; // Holds Velocity Y
new Float:svz[MAX_PLAYERS]; // Holds Velocity Z
new Float:s1[MAX_PLAYERS]; // This Is What Our Forumula Outputs
new s2[MAX_PLAYERS]; // This Is The Output When The Forumula's Ouput Is Round
new s3[MAX_PLAYERS][256]; // This Is The Text That Is Displayed To The User
new Text:sdisplay[MAX_PLAYERS]; // This Holds The Textdraw's ID
new stimer[MAX_PLAYERS]; // This Holds The Timer's ID (Used To Refresh Speed)

forward speedometer(playerid); // This Forwards The Timer To Our Function

public OnFilterScriptInit() { // This Is What Is Excuted When The FilterScript Starts
print(" ");
print(" ----------------------------------- ");
print(" ");
print(" Speedometer Filterscript Has Loaded ");
print(" ");
print(" ----------------------------------- ");
print(" ");
return 1;

public OnFilterScriptExit() { // This Is What Is Excuted When The FilterScript Ends
print(" ");
print(" ------------------------------------- ");
print(" ");
print(" Speedometer Filterscript Has Unloaded ");
print(" ");
print(" ------------------------------------- ");
print(" ");
return 1;

public OnPlayerConnect(playerid) {
sdisplay[playerid] = TextDrawCreate(10.0,200.0," "); // This Creates Our Textdraw And Stores The Id In The Varible In Front
TextDrawSetShadow(sdisplay[playerid],0); // This Removes Our Textdraw's Shadow ( Makes Easier To Read )
TextDrawSetOutline(sdisplay[playerid],1); // This Adds A Black Outline To Our Textdraw ( Makes Easier To Read )
TextDrawFont(sdisplay[playerid], 2); // This Changes Our Textdraw's Font ( I Think This Ones Makes It Easier To Read )
TextDrawShowForPlayer(playerid,sdisplay[playerid]); // This Shows The User The Textdraw
return 1;

public speedometer(playerid) {
GetVehicleVelocity(GetPlayerVehicleID(playerid), svx[playerid], svy[playerid], svz[playerid]); // This Saves Our Velocitys To Our Varibles
s1[playerid] = floatsqroot(((svx[playerid]*svx[playerid])+(svy[playerid]*svy[playerid]))+(svz[playerid]*svz[playerid]))*100; // This Is Our Forumula ( I Don't Know How It Works )
s2[playerid] = floatround(s1[playerid],floatround_round); // This Rounds Our Output To The Nearest Whole Number
format(s3[playerid],256,"%i MPH", s2[playerid]); // This Format Our Text Into What We See
TextDrawSetString(sdisplay[playerid], s3[playerid]); // This Changes The Value Of Our Textdraw To What We Formatted
return 1;

public OnPlayerStateChange(playerid, newstate, oldstate) { // This Is The Callback That Is Called When A Person Changes State ( ex States In_Vehicle, Dead, Passenger, Speculating... )
KillTimer(stimer[playerid]); // This Stops Our Timer For When You Get Out Of Your Vehicle Your Speed Doesn't Keep Going
TextDrawSetString(sdisplay[playerid], " "); // This Sets Our Textdraw To Blank And Freezes Because We Stop The Timer ^
if(newstate == 2) stimer[playerid] = SetTimerEx("speedometer", 255, true, "i", playerid); // This Starts The Timer When The Player Changes His/Her State To Being The Driver
else if(newstate == 3) stimer[playerid] = SetTimerEx("speedometer", 250, true, "i", playerid); // This Start The Timer When The Player Changes His/Her Start To Being The Passenger
return 1;

02/11/2010, 03:51 AM
Nice tutorial very useful

02/11/2010, 04:06 AM
It is nice, but i think it's very plain no offense :)

02/11/2010, 04:17 AM
That is why is said ' Basic Speedometer '

02/11/2010, 04:27 AM

03/11/2010, 10:44 PM
nice! but by the way there is already two speedo tutorials.... didn't you see them? :P

04/11/2010, 02:51 AM
Nice and simple.

PS: next time use [ pawn] [ /pawn]

04/11/2010, 03:04 AM

Very nice tutorial.

06/11/2010, 02:39 PM
Nice, Good for begginers.

06/11/2010, 02:49 PM
Good job, at every section you explain what that line of code does. Even though it's one bump of code in a single tag and the variables aren't fully optimized, this is brilliant. Good job!

08/11/2010, 11:50 AM
can u make tuturial with KM/H too??? because others need km/h too

13/06/2011, 03:58 PM
Next time, embed PAWN code in stead of PHP.

14/06/2011, 01:40 PM
Next time, embed PAWN code in stead of PHP.

Next time, check last post before posting :>

Nice tutorial though.

14/06/2011, 09:46 PM
I like this because it has been explained well, however there are already speedo tutorials! Try and make your tutorials a bit more unique! anyways.... 8/10 ;-)

14/06/2011, 09:48 PM
Well knowing beginners they will copy and paste but indeed nice tutorial I wonder if it is a accurate one that shows the real speed not like going from 10 to 25

04/07/2011, 03:30 AM
Well I'M super NEW to Scripting, Where exactly would i add this?

04/07/2011, 03:42 AM
Save it in your filterscript folder.
Don't forget to add it in your server.cfg

11/07/2011, 07:01 PM
Well this isn't a tutorial because of the fact it doesn't explain nothing AT ALL.

13/07/2011, 07:40 PM
Please photo

13/07/2011, 07:49 PM

14/07/2011, 05:57 AM
Well this isn't a tutorial because of the fact it doesn't explain nothing AT ALL.

You sure? Check again.

14/07/2011, 04:11 PM
It's very nice.I tried it out.
Love it. ^^

15/07/2011, 03:58 PM
I'd like to make it so it's in that same spot on the right side and a label above it, what would I adjust and how?

15/07/2011, 04:05 PM
Well this isn't a tutorial because of the fact it doesn't explain nothing AT ALL.

Yes it does. Actually look at the code itself.

04/11/2011, 09:11 PM
i'll try it.

05/11/2011, 11:16 AM
it's very nice , i would create one for my server , using a better and realistic textdraw.

05/11/2011, 11:38 AM
How to make this in km/h?

05/11/2011, 02:10 PM
mhm, a foto ?

05/11/2011, 04:13 PM
Great tutorial !

20/11/2011, 05:33 AM
very nice, i enjoyed the simplicity, because im new to scripting, so yeah....thanks

22/07/2013, 11:02 AM
Nice and helpfull +REP

23/07/2013, 02:56 PM
Hahahhaa nice one bro :)
But i got a question here, why each variable has [MAX_PLAYERS] in end of its script? :/

please answer thanks

23/07/2013, 03:32 PM
Very nice tutorial, but i got a question here,
why in end of variable, its always have [MAX_PLAYERS]?
What does it use for?

23/07/2013, 09:28 PM
It's using MAX_PLAYERS for it to be per-player. If it didn't, there would be 1 textdraw and it would end up being formatted with everyone's speed so will only show one.