New to lua and stuck on a nil value problem, probably isn't a hard fix.

Right so I can’t really find out what’s wrong with what I’ve written. This is only my 2nd day messing around with Lua and trying to get the hang of it, and I can’t really find out why this won’t work. I’ve tried looking through various items that I figured would have the solution but I can’t find anything and google isn’t being helpful.

[lua] SixShooter.DoClick = function(ply)
if ply:Alive() then
ply:ChatPrint(‘You must be dead to change your weapon!’)
RunConsoleCommand(“weapon_take”, “357”)
It keeps telling me that:

attempt to call method ‘Alive’ (a nil value)
[sp] Line 2[/sp]

I don’t really see how I can work my way around this since I pretty much do need the player to not be alive if they want to activate the button. If anybody can tell me what I’m doing wrong and perhaps why it’d be extremely helpful. Also excuse me if the lua is very poorly written, I don’t have much control over that at the moment.

ply is a nil value. If your running the code clientside replace ply with LocalPlayer() and dont pass it as a param to the function.

Also why spoiler line 2?

Line 2 was spoiler’d because I figured that if somebody didn’t understand which line the error was on they could use that to find out. I just feel it makes it easier to read for people who do know which error the line is on, though.

Also thanks, what you’ve said is working. Guess I’ll work on learning what goes where regarding clientside and… serverside :v:?

[editline]6th August 2011[/editline]

Actually that does help me with a lot more issues then my button one, thanks!

client is yellow, can be used client only.
shared is half yellow half blue, can be used client and server
server is blue, can be used server only.

Take a look through all those libraries, really useful stuff you’ll come across, trust me.

and menu is green

The server is the object that runs and verify’s everything. The client is used to make decisions, for example when a player walks or types +forward the client sends a console command to the server and the server checks if there is a wall infront of the player or if another object is there. If they player can walk meaning nothing is there and they arent frozen etc then it tells all the clients that you moved.

Also note that you should NEVER trust the client, what they say is always wrong but your gonna listen to them anyways but always verify what they say on the server.

ply is not nil, ply is the current panel, when using the button.DoClick = function methode the first argument in the function will ALWAYS be the panel, the player will never be passed to the DoClick event, I have no idea how you got that, also use LocalPlayer().

So wait, this is good for me too.
If I’m doing clientside lua, and do:
local ply = LocalPlayer()
than I can use ply, cause it’s defined.

And if I try to use ply in Serverside, it automatically means LocalPlayer() ? or Player()?

No. LocalPlayer is not defined serverside. Use ‘for k, v in pairs(player.GetAll()) do’ to run on all players.

Think of it this way.

The server has all the players the client only has one, that is you. For the server you need to have some kind of information used to find the player such as the name, steamid or unique id. There are lots of ways to find a player as you will see.