GMod Specific Lua Questions

I have a couple of questions about GMod specific Lua. (Lua in general I get for the most part)
If you can answer one or all of my questions it would be much appreciated.
It would be better if you could explain the answer to my question. Examples help but I’d like to learn how everything works, not just copy paste.

1: Can someone please explain to me how to make the server do an action on the player.
In example: If I want the server to give the player a weapon, or set his/her health. How would that be
done? I know it has somthing to do with this pl:(function) I see in nearly every lua file I see, but I have
no idea what that means or how it works.

2: Relating to question 1, What is this pl (I’ve seen it used as ply before), before many functions. I’ve
tried doing similar things but can never seem to get them to work. How does it work? When should I
use it? etc…

3: If a client calls a command that gives them a weapon or sets their health or somthing of such, will
everyone else be able to see said weapon or health change? It was triggered on the client which is all
local so wouldn’t that mean that the health or weapon would just be for said client to see?

4: Can I set/check a global variable from a client-side file?
More Detail: Let’s say I have a table and in which is an entry for each player that will in theory keep
track of the players money. Will I be able to set/check the value that is on the server side from the
client? So that if the value for said player is not high enough, they won’t be able to complete an action?

5: Relating to question, Is there a better way to keep track of a value for a certain player? Better then
assigning a player a certain entry in a table then altering it as he/she completes or fails certain actions.

Sorry for all the questions, but I’m trying my best to learn, and I can’t progress too much farther without figuring these things out.
Thanks!

Console commands
**[Concommand.Add

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Concommand.Add)**
2.Pl or ply is the player this could be the local player if on client or the player that your running lua on from server
3. You cant give guns or set health from client unless you have a console command on server and it will only effect the players or player you tell it to
4. You can only set it if you make something on server to set it for example a console command but you can check it on client with out doing this
5. Dont really understand what your saying but if im reading it correct then what you want is usermessages http://wiki.garrysmod.com/?title=User_Messages

OK. Thank you very much. One new question I just thought of and a few about your answers.

The questions about your answers are as follows:
1a: Is there no other way except making the client run a console command to do the action on him/her?
1b: If there is indeed no other way, how can I protect the client from not being able to just bring up the console, type in the command, and cheat their way to the top?
2: OK, Well that tells me what it is. But I’m still confused about how to use it. How would Lua know which player I’m talking about? Is there a certain parameter or something?
3: OK, Console command. But will other players be able to tell that said player has Gun X, or Y Health, or will it just be on the client that ran the commands screen?
4a: OK. That answers that.
4b: Is there a specific way to check globals on the client side?
5: I’ll look into user messages thanks.

Everything isn’t console commands, don’t believe him :sigh:

When you act on something that affects the game globally or progress within the game, you should handle all of that onthe server. There are specific commands from the server that act on clients, and you mist call these from the server. Examples are when you give a player a weapon, you use Player.Give (or ply:Give) to give a player a weapon. Using consolecommands would be completely idiotic, especially for things as important as weapons. The server should handle any kind of important variable handling for player, and you should only send the variables one-way to the client through Usermessages. This makes sure that every client keeps consistency and keeps lag at a minimum. For the third question, yes, any client can tell how much health or what weapons another client has. This is for prediction, or the process of your game predicting the outcome of important game events such as what gun another person pulls out, and allows you to view the health of other players like in gamemodes such as Sandbox. Yes, you can check globals on the client through concommands, but this is strictly for non-important variables or variables that, if changed from an expected value, will not affect the process of the game.

If you have any other questions as simple as yours, I suggest you add me or another one of the more experienced coders here and I’m sure that they’ll be happy to help :wink:

[editline]03:40PM[/editline]

Please ignore spelling mistakes, I’m typing on an iPhone since I’m currently not at home.

The questions about your answers are as follows:
1a: Is there no other way except making the client run a console command to do the action on him/her?
Well you could do a check for what ever gives him gun for example how many kills he has if he has 5 then give him the gun or you wanted to be able to give players gun then you could do a check to see if player is admin
1b: If there is indeed no other way, how can I protect the client from not being able to just bring up the console, type in the command, and cheat their way to the top?
depends how you are setting health and giving guns
2: OK, Well that tells me what it is. But I’m still confused about how to use it. How would Lua know which player I’m talking about? Is there a certain parameter or something?
Sometimes the value of ply or pl is for example
**[Gamemode.PlayerSpawn

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.PlayerSpawn)**
ply is the player who just spawned
But you can also define who it is by a variable for example
ply = LocalPlayer()
3: OK, Console command. But will other players be able to tell that said player has Gun X, or Y Health, or will it just be on the client that ran the commands screen?
clients can find out the players health
**[Entity.Health

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Entity.Health)**
or
**[Player.GetActiveWeapon

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Player.GetActiveWeapon)**
to get what gun player has
4a: OK. That answers that.
4b: Is there a specific way to check globals on the client side?
The best method would be send a usermessage to client or you could use
**[Entity.GetNetworkedEntity

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Entity.GetNetworkedEntity)**
5: I’ll look into user messages thanks.
[/QUOTE]

OK. That just about does it for that. Gbps, I’ll talk to you on steam when you get home. It will be much faster then talking on the forums. Expect a lot of questions since you seem to know what your doing. : )
THANK YOU!

Please don’t confuse him, it’s best you explain it as if you were teaching than if you’re chatting.

[editline]03:47PM[/editline]

I’m here to help. I was in the same spot as you at one point, so I try to help as much as possible. It’s nice to see someone who actually tries hard to learn GLua these days.

Also just to note I completely misunderstood what he said that is why i suggested console commands looking back over it I was clearly wrong

That’s OK. You tried to help which is all I can ask for. :slight_smile:

It’s ok, just make sure that you actually know what you’re teaching and how to teach it before you try to teach it. :geno:

This is why I reply to help people because I learn something new I dont mind being proven wrong as long as im informed of what im doing wrong and how I should be doing it