The online racing simulator
[Solved] Save/Get driven distance from database
Hi, I would like to ask you for help since I do not remember how to do so that the total distance that the user has running on the server appears, only shows me a distance that when I close LFSLapper is restarted, what I want is for the distance to count the total That the user takes on the server

This is my code

<?php 
openPrivButton
"distancia3",47,0,15,5,5,-1,32,"^7Distancia total: ^6" Round(ToNum(GetCurrentPlayerVar"KM" ) + GetCurrentPlayerVar("TotDistMeter") / 1000 ),1));
?>

Thanks in advance!
To do that, you have to store the value when the user disconnects and retrieve it from the database when the user connects again.
How do I do that? :S

Thanks for answering Yisc[NL]
In the OnConnect event you need to retrieve the value using 'getuserstoredvalue'.
In the OnDisconnect event you need to store the value using 'setuserstoredvalue' or save it when passing start/finish
I have no idea how to do that, since I never use the storedvalue :/
Look in folder 'Doc' for file 'changes.txt', you can find explanation there.
Otherwise look at my or other peoples Lapper script to see how to use it.
It would be something like


<?php 
SetStoredValue
("Distancia_" $track "_" $car "_" $username$Dist);
?>

Yeah, that would work perfectly
I use it that way but it did not work


<?php 
Event OnConnect
$userName # Player event
    
GetStoredValue("Distancia_" $track "_" $car "_" $username$Dist);
EndEvent
?>



<?php 
Event OnDisConnect
$userName$reason # Player event
    
SetStoredValue("Distancia_" $track "_" $car "_" $username$Dist);
EndEvent
?>



<?php 
Sub nodoetc
$KeyFlags )
openPrivButton"distancia3",47,0,15,5,5,-1,32,"^7Distancia total: ^6"GetCurrentPlayerVar "Dist" ),"" );
PrivDelayedCommand1nodoetc );
EndSub
?>


And what did you to to debug your code?
My answer is not meant to be annoying, but learning to debug code is essential to become a better code writer.
A few thing you could check in order of importance:

- Is the value actualy stored in the database when a player disconnects?
- If above question can be answered with 'yes', is the value correcly retrieved when a player connects?
- If that answer is 'yes' as well, then there must be an issue in the sub that is supposed to show the button
I have this error in the log


<?php 
7
/20/2017 5:31:02 AM -> Syntax error in cfg file ".\default\SISTEMAS_TWEAK.lpr" at line #158
    
';' needed
    
Function 'onconnect' script aborted
?>


Add a semicolon (';') to the end of line 157. (Your error says line 158, but generally when missing a semicolon it is the line before that has one missing)
Quote from Racon :Add a semicolon (';') to the end of line 157. (Your error says line 158, but generally when missing a semicolon it is the line before that has one missing)

This is the error line


<?php 
GetStoredValue
("Distancia_" $track "_" $car "_" $username$Dist);
?>

Although the error suggests that a semicolon is missing, which can be the real problem in some cases, that same error is also given when an empty value is inserted, which causes "" get together and is read as a closure of a piece of code.
Add this to your code and see what is actualy written on the console of Lapper:

writeline ( "Distancia_" . $track . "_" . $car . "_" . $username );
Quote from Yisc[NL] :Although the error suggests that a semicolon is missing, which can be the real problem in some cases, that same error is also given when an empty value is inserted, which causes "" get together and is read as a closure of a piece of code.
Add this to your code and see what is actualy written on the console of Lapper:

writeline ( "Distancia_" . $track . "_" . $car . "_" . $username );

Thank you very much for replying, another inquiry ...

To get the distance from the GetStoredValue, is this line okay?

<?php 
openPrivButton
"distancia3",47,0,15,5,5,-1,32,"^7Distancia total: ^6"GetCurrentPlayerVar "Dist" ),"" );
?>

I'm sure we have told this many times , but oke. Another one..........

If you want to get/set a Global variable to/from the database you have to use the following functions:

Get/Set Global Var

SetStoredValue("varname", $value) - sets a variable
GetStoredValue("varname") - returns variable as a string
GetStoredNum("varname") - returns variable as a numeric (or as -1 if it's not a number), so you don't need to do a ToNum($var) conversion if you're doing numeric comparisons

Get/Set Player Variables like storing the totaldistance of each user, you have to use these functions.

Get/Set User var

SetUserStoredValue($username, "varname", $value) - Sets a variable
GetUserStoredValue($username, "varname") - returns variable as a string
GetUserStoredNum($username, "varname") - returns variable as a numeric

To set the Player's distance , go to the Player event: OnDistDone. This event will execute every 100 meters the player has driven.
Event OnDistDone( $userName ) # Player event
EndEvent

In that event you STORE the total distance.
SetUserStoredValue($username,"TotalDistance",GetUserStoredValue($username,TotalDistance)+100);

I'm sure you know now, how to GET the player's stored distance.
This thread is closed

FGED GREDG RDFGDR GSFDG