Yet more trouble with tables ;_;

I’ve been trying everything I can to get this code to work but it just won’t, I’ve traced the problem back to it’s roots and I’m confused.


playermoney = {} //Table stores the amount of money each player has.


function GM:PlayerInitialSpawn( ply )
	ID = ply:UniqueID()
	playermoney[ID] = 0
	print("ID is " .. ID)
	print("Player " .. ID .. "'s Money is " .. playermoney[ID])
	print("Player 1's Money is " .. playermoney[1])
end

The above code prints the following:

ID is 1
Player 1’s Money is 0

[ERROR] gamemodes/horror/gamemode/init.lua:29: attempt to concatenate a nil value

  1. unknown - gamemodes/horror/gamemode/init.lua:29

How can playermoney[1] be a nil value when I have set playermoney[ID] to 0 with ID being equal to 1? Thanks to anyone who can help, I really want to get my head around tables and none of the lua documentation has seemed to help me understand.

Is that code the exact code that is being ran?

Just do



function GM:PlayerInitialSpawn( ply )
   ply.Money = 0
end


I doubt you’ve shown us the entire code though, that should work just fine (assuming ‘ID’ is 1).

[editline]4th November 2013[/editline]

Also, are you in singleplayer? ‘UniqueID’ shouldn’t ever really return 1 unless you’re on singleplayer (I think).

Yeah I am in single player at the moment :slight_smile:
And really, you it should work? Okay, there is other code that is running but I’m fairly certain that none of it is related to the money code, I guess I’ll try running just this code alone to check whether it works then. Thanks :slight_smile:

never test your stuff in singleplayer( generally, there is a few exceptions ). Just create a multiplayer game, your ports don’t have to be open or anything.

I highly suggest you skip what you’re doing and look into what i posted in your other help thread, even though you don’t understand it. You should take the time to look through it and try to understand what is going on, if you need help i always allow people to add me to steam for help.

What the script does is show an example of how you can easily create a persistent money script, so all players have money that saves even when they disconnect from the server and rejoin. and when the server crashes.

Having a global table that stores players money is not an efficient way to do what you’re trying to do. Also, you won’t be able to render your money on the hud or any clientside stuff, because you’re storing it only on the server.

-snip-

I think I know what your problem is, “UniqueID” returns a string and you’re trying to access it with its integer index. Either:


local ID = tonumber( ply:UniqueID() )

or


print( "Player 1's Money is " .. playermoney[ "1" ] )

should work.

I thought that too, but it’s supposed to return an integer.

“Supposed to” and “does” are 2 different things.


print( type( LocalPlayer():UniqueID() ) )

prints “string”.

use player:SteamID(), apparently player:UniqueID() is harder to calculate. use to be that way anyway but i don’t see how it’d be different

Why not just the player entity itself? ply.Money = amountofmoney

Also there’s something pretty wrong about your understanding of tables (Your method would work, but it’s super weird and impractical). You should read this up: http://lua-users.org/wiki/TablesTutorial

like i said he’s doing things really inefficient, that’s why i mentioned what i posted on his other thread

Yeah you’re right guys, Thanks for coping with me xD I worked out that unique i.d returns an integer and now that thats been fixed I’ve changed it to create tables for individual players i.e ply.money so it’s all good thank you all, I promise I won’t keep making noobie errors i just like to throw myself into complicated things, I’m definitely getting the hang of lua coding though.

Yeah thanks for all the advice LauScript, I will definitely look at the code and learn about how it works, at the moment it just feels like an achievement to write some code which does the job, also I used user messages to display and update the money for each player on their HUD but I guess there’s probably a more efficient way to do that too :stuck_out_tongue: Would you mind if i added you on steam?

be my guest like i said i always allow people to add me and i’ll always be glad to help with questions related to lua

-snip- (It’s a string, not a number; Evacx is right.)