Usermessage Into Variable Not Working

I’m trying to create a newer style of zombie survival gamemode and my usermessages aren’t playing ball. The data is being sent correctly because the 1st two lines of code work as they should it’s the last 2 lines don’t work for some reason.
By my guess it should have the same value because it’s just a variable thats being set from the usermessage long.



    Msg(data:ReadString()..": "..data:ReadLong().."
")
    Msg(tostring(type(data:ReadString())..": "..type(data:ReadLong()).."
"))
    TimeLeft = tonumber(data:ReadLong())
    Msg(TimeLeft.."
")


Output:



update: 10
string: number
0


Also if I swap the lines for instance:



    TimeLeft = tonumber(data:ReadLong())
    Msg(TimeLeft.."
")
    Msg(data:ReadString()..": "..data:ReadLong().."
")
    Msg(tostring(type(data:ReadString())..": "..type(data:ReadLong()).."
"))


I get this:



-1027022614
te: 280
string: number


Could someone please explain what I’m doing wrong here??

We have tried to work out the “Debug.Debug” or “Debug.*” in Lua… but not sure how to work this either… Its odd… because the lua code is right… but its not working correctly?.. some of the things in lua dont make logical sense but still work/ dont work… any ideas people?

[lua]
local string=data:ReadString()
local long=data:ReadLong()
[/lua]
Use variables for the initial data reading instead of continuously using data:Read___(), as the game reads a usermessage in order. Essentially, the second two Read commands aren’t getting any data.

You should probably read up on usermessages on the wiki:

That worked perfectly, Thanks mate.

Thats worked. Thank you! I didn’t expect lua to be so picky about accessing things from functions etc. Theres nothing on the wiki about it, only that they need to be accessed in the order their sent.

It’s not lua, it’s how the library works.

Also so you don’t make the same mistake again, the function type returns a string of that the variable inputted is;

Example:



print(type(data:ReadLong()));    //Will print "number"
print(type(data:ReadString()));   //Will print "string"
print(type(data:ReadBool()));     //Will print "bool"


Instead you would do:



print(tostring(data:ReadLong()));    //Will print the number recieved
print(data:ReadString());   //Will print the string recieved


But when it comes to usermessaging you want to define a variable to reference the data and not call it directly.



Example:

local recv_number = data:ReadLong();
local recv_string = data:ReadString();

print(recv_number, recv_string);


Then build off those variables for whatever you are doing.

A wiki of the type function here.
A wiki tutorial can be found here.