• Print

Author Topic: Function help  (Read 4255 times)

0 Members and 1 Guest are viewing this topic.

Offline Kaleesh

  • Newbie
  • *
  • Posts: 2
  • Karma: -1
Function help
« on: September 07, 2015, 08:01:47 am »
I'm trying to set a function to check if a players name is bill and if it is, set his armor and health to 500, can I get some help? Ty

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Function help
« Reply #1 on: September 07, 2015, 08:33:56 am »
You'll probably want to take a look at a few things.
If you need help with hooking events, ie PlayerSpawn, it's simple:
Code: Lua
  1. function yourCallbackFunctionNameHere(argName1, argName2, ...)
  2.     -- your code here
  3. end
  4. hook.Add("EventNameHere", "YourUniqueHookIdentifierHere", yourCallbackFunctionNameHere)

EDIT: Or, alternatively, you could use this anonymous-function syntax for your hook.
Code: Lua
  1. hook.Add("EventNameHere", "YourUniqueHookIdentifierHere", function (arg1, arg2, ...)
  2.     -- your code here
  3. end)
« Last Edit: September 07, 2015, 08:47:21 am by Bytewave »
bw81@ulysses-forums ~ % whoami
Homepage

Offline Kaleesh

  • Newbie
  • *
  • Posts: 2
  • Karma: -1
Re: Function help
« Reply #2 on: September 07, 2015, 09:25:16 am »
do you know the full code of it, and if so can you write it out, I'm very noobish at coding, if you can, that would be gr8

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Function help
« Reply #3 on: September 07, 2015, 09:55:34 am »
If you're going to be doing this kind of stuff, I always have to recommend learning how it works and not asking people to write it for you. I understand you're nooby at programming, but we all were once.

I'm going to try and break it down for you as easily as possible.
PlayerSpawn is an event. It's called whenever a Player spawns in the map alive (not as a spectator). It returns one variable, the Player entity of the spawned player.
Player:Name() gets the Player object's Steam name. You might not want to do this, actually, and instead opt for Player:SteamID(), which can never change.
Entity:SetHealth(number health) does what it says. It sets an Entity's health (in this case a Player's) to the number you pass it.
Player:SetArmor(number armor), again, does exactly what it says. It sets a Player's armor to whatever you give it.
Hooking events, as I said, is rather simple. In your case, you'd want to place code for hooking things in some Lua file in /lua/autorun/server/ in your server's files. This is because all of these functions exist on the SERVER side of the Lua instance, so it's all SERVER sided calls.

The code for hooking PlayerSpawn would look something like this.
Code: Lua
  1. hook.Add("PlayerSpawn", "DoSomething", function(ply)
  2.     -- your code here
  3. end)
If you wanted to print a message to the server console with a player's name every time someone spawns, it'd look like this.
Code: Lua
  1. hook.Add("PlayerSpawn", "PrintPlayerNameOnSpawn", function(ply)
  2.     if not IsValid(ply) then return end
  3.     print(ply:Name() .. " has spawned!")
  4. end)
In that code is also an example of using Player objects. The "callback" function you give the hook accepts one parameter, that parameter being the Player who spawned. I assigned a name to that variable, ply, but really it could be anything.
The line:
Code: Lua
  1. if not IsValid(ply) then return end
can be summarized as "If the player object I got isn't actually a player (bug/disconnect?), stop running".

Moving onto conditionals, like what you're asking, is very simple. If I wanted to print a message only when a player with a certain name spawns, like you're asking, it's also very easy to do.
Code: Lua
  1. hook.Add("PlayerSpawn", "PrintPlayerNameOnOneGuy'sSpawn", function(ply)
  2.     if not IsValid(ply) then return end
  3.  
  4.     if ply:Name() == "Some Guy" then
  5.         print("Some Guy has spawned!")
  6.     end
  7. end)
Again, however, names can change. I would recommend using a SteamID instead, which is static. That would look something like this.
Code: Lua
  1. hook.Add("PlayerSpawn", "PrintPlayerNameOnOneGuy'sSpawn", function(ply)
  2.     if not IsValid(ply) then return end
  3.  
  4.     if ply:SteamID() == "STEAM_0:1:44419830" then
  5.         print("Bytewave has spawned!")
  6.     end
  7. end)

The way to use the methods you want to call is just like calling ply:SteamID() or ply:Name(), except you'll ahve to pass in a parameter. That looks something like this.
Code: Lua
  1. ply:SetHealth(100) -- give the player 100 HP
  2. ply:SetArmor(100) -- give the player 100 Armor

The rest should be fairly easy to figure out; I've given you the pieces to the puzzle, now put them together. :)
bw81@ulysses-forums ~ % whoami
Homepage

  • Print