• Trying to change player team on death
    53 replies, posted
I'm trying to make it so that when someone who is on TEAM_MAYOR is killed they change team to TEAM_CITIZEN. Can anyone help? This is what I have so far. [lua]hook.Add( "PlayerDeath", "playerDeathTest", mayorcheck ) function mayorcheck() Victim:Team team.GetName if team.GetName == TEAM_MAYOR then GM:PlayerJoinTeam(victim, TEAM_CITIZEN) end end [/lua]
I think you need to take a crash course in Lua syntax or look over some other codes before doing some of this. There's a lot wrong with your code. 1. Your hook will fail, because you add it before you define the function 2. Your function will fail, because you never pass any arguments so there is no "Victim" 3. You don't use function notation with most of the functions called within your hook, so you'll get errors there. 4. You never assign Victim:Team() to a variable, so it's just floating out there in space. 5. team.GetName has no arguments and is not assigned to a variable. 6. TEAM_MAYOR is not the name of the team, it's the enumeration correspoding to the team number. You're supposed to compare player:Team() with the enum, not team.GetName. 7. Even if you did pass a "Victim" argument to the function, it would still fail because you used the variable "victim" in PlayerJoinTeam. Lua is case sensitive. 8. The syntax for using gamemode functions is GAMEMODE:Function.
[lua] function MayorCheck(victim,killer) if victim:Team() == TEAM_MAYOR then victim:SetTeam(TEAM_CITIZEN) end end hook.Add("PlayerDeath","MayorCheck",MayorCheck) [/lua]
Thanks for your help. I'm trying to make things whilst learning, but it's not going too well :)
[QUOTE=Bigdogbikers;19515075][lua] function MayorCheck(victim,killer) if victim:Team() == TEAM_MAYOR then victim:SetTeam(TEAM_CITIZEN) end end hook.Add("PlayerDeath","MayorCheck",MayorCheck) [/lua][/QUOTE] I put this in addons/mayor/lua/autorun/client and then made a blank lua in /server and /shared. However when I go in singleplayer or into a listen server when I die as the mayor I don't get demoted. :S
[QUOTE=sintwins;19525226]I put this in addons/mayor/lua/autorun/client and then made a blank lua in /server and /shared. However when I go in singleplayer or into a listen server when I die as the mayor I don't get demoted. :S[/QUOTE] umm it's not clientside?
Woops I put it in shared, not client. Is it ok to be in there?
No. It's [b]serverside[/b].
Lol. I fail. Thanks But doesn't the server run shared.lua ?
It does, but it also runs on the client. Which is pointless. Only things that need running on the client and server should be shared.
Ok thanks. I put it in shared but it still doesn't work :( Everything I try to do with lua fails lol
[QUOTE=sintwins;19525526]Everything I try to do with lua fails lol[/QUOTE] That's because you are trying to run before you can walk. You need to have a more full understanding of the syntax and paradigms of Lua before you even think about starting to code with it.
[QUOTE=NullPoint;19525565]That's because you are trying to run before you can walk. You need to have a more full understanding of the syntax and paradigms of Lua before you even think about starting to code with it.[/QUOTE] Well I read through the syntax of lua on the wiki and understand all of that and I read all of the vgui tutorials. But I don't know what I can do now to learn, so I've been trying to make things and see where I go wrong and learn that way :P EDIT - How would you suggest that I learn? At the moment I am following SWEP tutorials and then trying to change parts of it.
Put it in init.lua. Don't mess with SWEPs, pointless. Work more on gamemode side of stuff.
Thanks :D I'll start looking through some gamemodes, maybe DarkRP :)
Fretta gamemodes would probably be best, Fretta is a nice base and most gamemodes are very simple. But really console commands are the easiest way to learn Lua, you can do a lot with them and they are very simple.
What do you mean? Like use console commands to do things ? Like to kill a player make them type "kill" in console?
Sure, player:ConCommand("kill") (replacing player with the name of your player variable of course)
ok thanks. Also I was wondering if I know the syntax of Lua well enough to start learning Glua. I read all of the Lua tutorials on teh wiki and some of the Lua documentation, but is this enough? Does the wiki have all of the information that I need or should I read the rest of the manual?
[QUOTE=MegaJohnny;19531047]Sure, player:ConCommand("kill") (replacing player with the name of your player variable of course)[/QUOTE] Of course, there is no point in using player:ConCommand("kill") when you can use player:Kill()
[lua]local function MayorCheck(victim,killer) if victim:Team() == TEAM_MAYOR then victim:Kill() victim:SetTeam(TEAM_CITIZEN) end end hook.Add("PlayerDeath","MayorCheck",MayorCheck)[/lua]
Why would you kill someone in a PlayerDeath hook?
No, I didn't mean to use the concommands to do things like that. I meant making console commands are a lot easier than gamemodes and will help you learn Lua, don't start out by trying to make a roleplay gamemode.
[QUOTE=CowThing;19531894]No, I didn't mean to use the concommands to do things like that. I meant making console commands are a lot easier than gamemodes and will help you learn Lua, don't start out by trying to make a roleplay gamemode.[/QUOTE] Ok, I'll start with that. Any easy console commands that you suggest I try first?
say "Im a noob"
[QUOTE=MegaJohnny;19531047]Sure, player:ConCommand("kill") (replacing player with the name of your player variable of course)[/QUOTE] SERVER_CAN_EXECUTE bro
Ok, I'll make a command say "I'm a noob" :) Hopefully I Will be able to do this lol. And this will go in lua/autorun/server? EDIT- Is there a hook to find the caller of a console command? Thanks
[B][URL="http://wiki.garrysmod.com/?title=Concommand.Add"]Concommand.Add [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG][/URL][/B] Your arguments for the function is "player, command, arguments", player is the player who called the command. concommand.Add is shared so you can make both server-side and client-side commands.
[sp] concommand.Add("noob", function(ply) ply:ConCommand("say I'm a noob \n") end ) [/sp] dont click it unless you get stuck :aaaaa:
Ok, like this? [lua]function imnoob (player, command, arguments) -- creates function player:ConCommand("say I'm a noob") -- says I'm a noob end concommand.Add("inoob", imnoob) [/lua]
Sorry, you need to Log In to post a reply to this thread.