Running a command when a player Changes team.

I’m editing DarkRp cause I’m bored right now and I’ve run into a bit on trouble. I want a menu to popup when the player becomes a Mayor. This is what I have so far

[LUA]
function GM:OnPlayerChangedTeam( ply, oldteam, newteam )
if ( newteam == TEAM_MAYOR ) then
print(“ItWorked”)
end
end
[/LUA]

I didn’t really change anything from the function from the wiki and ItWorkd doesn’t show up and console and I am not getting any errors. Any help would be appreciated.

[lua]function TeamChanges( ply, oldteam, newteam )
if ( newteam == TEAM_MAYOR ) then
print(“ItWorked”)
end
end
hook.Add(“OnPlayerChangedTeams”, “TeamChangeHook”, TeamChanges)[/lua]

Top of my head… sorry, Im still hungover from last night :b

Hmmm, ItWorked still doesn’t show up in Console.

Have you enabled respawning on team change?

I’ve tried it with and without re-spawning on team change and it still doesn’t work.

try this:

[lua]
function GM:OnPlayerChangedTeam( ply, oldteam, newteam )
print(TEAM_MAYOR, newteam)
end
[/lua]

It will print 2 numbers to console, check if they are the same.

if you use GM:<Hookname> you will override the default gamemode hook and probably break a lot of things
[lua]
function fpopup(ply, oldteam, newteam)
if newteam == TEAM_MAYOR then
print(“It fucking worked”)
end
end
hook.Add( “OnPlayerChangedTeam”, “ShowPopup”, fpopup ).[/lua]

Your try and _nonsense try’s are wrong because they used the GM table, rather then an hook Freze his try is wrong because he used OnPlayerChangedTeams instead of OnPlayerChangedTeam

Didn’t print anything.

If you are worried about overriding base gamemode functions it is better to just call the base function in your function, like so:

[lua]function GM:OnPlayerChangedTeam(ply, oldteam, newteam)
if newteam == TEAM_MAYOR then
– Do something
end
self.BaseClass.OnPlayerChangedTeam(self, ply, oldteam, newteam)
end[/lua]

Doesn’t seem to work.

With change team respawning enabled

[lua]
function StuffOnChangeTeam(ply)
if ply:Team() == TEAM_MAYOR then
print(“itWorks”)
end
end

hook.Add(“PlayerSpawn”, “stuff”, StuffOnChangeTeam)
[/lua]

I tried yours and it doesn’t break the game mode, but it doesn’t print anything when I change do something to print(“test”)

Try printing something outside of the if statement to check if the function is actually being called, if it is then print(newteam == TEAM_MAJOR) to see if they are actually changing to the major team.

I tried to print something before and after the If statement and I got nothing.

Then the function isn’t even being called, have you got any addons that use the OnPlayerChangedTeam hook and is returning a value? Any return value will prevent any other OnPlayerChangedTeam hook and the gamemode function from running.

No sir, the only addon I have is Luapad.

Does the gamemode you are editing have a GM.PlayerJoinTeam function? If so, can you paste the code here?

I did searched all the files and I couldn’t find anything like that. And the Game mode I’m editing is DarkRp