• Interacting with a non-local player.
    10 replies, posted
Hey people. I have a problem when I code general gamemode things. Is it DarkRP customization or my own it doesn't matter. I come across places where I have to add some code that interacts with other players, and not the local player. A simple example: I want the GM:PlayerSpawn hook to kill the player with the most k's in their name. (or something like that) Or maby I want to reset all the teams of the players when one player (the local player) respawns. How would I interact with other players like this?
Interacting with the 'local' player only works in clientside code. When run on the serverside there is no 'local' player. This should be ran serverside, eg. by placing it in lua/autorun/server [lua]hook.Add("PlayerSpawn", "MySpawnFunc", function(ply) --ply is the player spawning --do something to them end)[/lua]
Well this is not really what I mean. If I do something like: [lua] function GM:PlayerSpawn(ply) ply:Give( "weapon_crowbar" ) end [/lua] this gives a crowbar to the player spawning. But what if I wanted to give everyone on the entire server 1 crowbar every time any random person spawned?
Use [.lua][/lua] tags and you could just do this: [lua] local weaponToGive = "weapon_crowbar" function GM:PlayerSpawn(ply) for k,v in ipairs(player.GetAll()) do if !v:HasWeapon(weaponToGive) then v:Give(weaponToGive) end end end [/lua]
[QUOTE=brandonj4;40921636]Use [.lua][/lua] tags and you could just do this: [lua] local weaponToGive = "weapon_crowbar" function GM:PlayerSpawn(ply) for k,v in ipairs(player.GetAll()) do if !v:HasWeapon(weaponToGive) then v:Give(weaponToGive) end end end [/lua][/QUOTE] Okay nice. This is more like it - I think. The problem is that I don't fully know how that code works. And I really want to learn instead of memorize. A noob-safe explanation of all that code would be golden.
[lua] local weaponToGive = "weapon_crowbar" --Create a variable for the class of the gun we want to give function GM:PlayerSpawn(ply) --The hook that we're going to use for k,v in ipairs(player.GetAll()) do --This will loop through all the players in the server if !v:HasWeapon(weaponToGive) then --Check if the player already has the weapon v:Give(weaponToGive) --If the player doesn't have the weapon then give them the "weaponToGive" end end end [/lua]
Okay awesome! It seems to me like you know a lot xD Can I do things the same way to specific players. So say if someone died, the other team could all have RPG's Or if someone died, one specific person could have an RPG.
[QUOTE=Kayvoo;40921700]Okay awesome! It seems to me like you know a lot xD Can I do things the same way to specific players. So say if someone died, the other team could all have RPG's Or if someone died, one specific person could have an RPG.[/QUOTE] Do you mean something like, if a person is a VIP on the team or a leader or something and they die, give everyone on the other team a specific weapon? Do you already have teams setup?
[QUOTE=brandonj4;40921764]Do you mean something like, if a person is a VIP on the team or a leader or something and they die, give everyone on the other team a specific weapon? Do you already have teams setup?[/QUOTE] Yeah. Not exactly, but if you explain that code then I think it will help me understand even more. So yes... that right there would be a brilliant example.
Untested: [lua] local Player = FindMetaTable("Player") --Get the Player MetaTable if !Player then return end --If the MetaTable doesn't exist then stop here function Player:SetLeader(bool) --Set the player to a leader somewhere self.IsLeader = bool end function Player:IsLeader() --Function to retrieve if a player is a leader return self.IsLeader end if SERVER then local specialWep = "weapon_rpg" --Weapon class that we want to give if the Leader dies function GM:DoPlayerDeath(ply, attacker, dmginfo) if ply:IsLeader() then --Check if we're a leader local plyTeam = ply:Team() --Store the team of the leader that died for k,v in ipairs(player.GetAll()) do --Loop through all the players in the server if v:Team() != plyTeam and v:Alive() then --Check to see if the team of the players that we are --looping is not equal to the leader that died and if they're alive if !v:HasWeapon(specialWep) then --Check if the player already has it v:Give(specialWep) end end end end end end [/lua]
[QUOTE=brandonj4;40921818]Untested: [lua] local Player = FindMetaTable("Player") --Get the Player MetaTable if !Player then return end --If the MetaTable doesn't exist then stop here function Player:SetLeader(bool) --Set the player to a leader somewhere self.IsLeader = bool end function Player:IsLeader() --Function to retrieve if a player is a leader return self.IsLeader end if SERVER then local specialWep = "weapon_rpg" --Weapon class that we want to give if the Leader dies function GM:DoPlayerDeath(ply, attacker, dmginfo) if ply:IsLeader() then --Check if we're a leader local plyTeam = ply:Team() --Store the team of the leader that died for k,v in ipairs(player.GetAll()) do --Loop through all the players in the server if v:Team() != plyTeam and v:Alive() then --Check to see if the team of the players that we are --looping is not equal to the leader that died and if they're alive if !v:HasWeapon(specialWep) then --Check if the player already has it v:Give(specialWep) end end end end end end [/lua][/QUOTE] Okay awesome! I believe this is just what I am looking for. Thankyou so much! :D
Sorry, you need to Log In to post a reply to this thread.