Get player team name in GM:PlayerInitialSpawn

Hello. I’m quite new to lua and I started making a gamemode based on the default sandbox gamemode. What I’m trying to make is a message saying “(player name) spawned in the server. Rank: (team name)”. This is what I have so far:



function GM:PlayerInitialSpawn(ply)
  PlayerDataUpdate(ply)
  PrintMessage(HUD_PRINTTALK, ply:Name() .. " spawned in the server. Rank: " .. team.GetName(ply:Team()))
end


However, when someone first spawnes in the server it always says “Rank: Joining / Connecting”. I have all my groups set up and teams assigned to them but I just can’t get the team name to display in that message. Are the teams assigned after this function? If yes, then what should I do?

This is because you’re overwriting the function.
You need to use a hook, like so:

[lua]
hook.Add( “PlayerInitialSpawn”, “SpawnMessage”, function(ply)

PlayerDataUpdate(ply)
PrintMessage(HUD_PRINTTALK, ply:Name() .. " spawned in the server. Rank: " .. team.GetName(ply:Team()))

end)
[/lua]

I tried this but it still doesn’t work. It just shows this:

http://img192.imageshack.us/img192/6700/joiningconnecting.png

And the name of the team in which I am is Head Admin:

http://img171.imageshack.us/img171/2800/2011101900001.jpg

The problem is that it shows “Joining / Connecting” instead of “Head Admin”. Here’s my code:
[lua]
function SpawnMessage(ply)
PrintMessage(HUD_PRINTTALK, team.GetName(ply:Team()) … " " … ply:Name() … " spawned in the server.")
end
hook.Add(“PlayerInitialSpawn”, “SpawnMessage”, SpawnMessage)
[/lua]

Set the team first.

It’s using UTeam from the ULX admin mod thing and I don’t know when it sets the team or anything.

How are you setting the player’s team? Are you doing it in your own script, or are you using something like ULX?

I don’t have anything about teams in my script. It’s all ULX.

Yeah, you ninjad my post.

I’m wondering if ULib has any hooks that are called after it’s done doing it’s stuff.

[editline]19th October 2011[/editline]

Are you actually using the UTeam script, or are you using the new team system built into ULX?

the one built into ULX

In that case I can’t think of a good solution. I would look and see if there is something that ULib provides that runs code after it’s set its teams.

I think I’m giving up on this -.-

Just put

[lua]timer.Simple(0.5, function()[/lua]

at the beginning of your line 2 and

[lua]end)[/lua]

at the end of your line 2.

OMG Thank you! :smiley:

Why not find where it sets the players team and just do the printmessage loop there?

Because I didn’t feel like going through every ULX lua file and looking for it?

That method is inefficent.

OP try using this, it should be a little faster and more efficient.

[lua]
local _pszPlayer = FindMetaTable(“Player”);
local _cacheMethod = _pszPlayer.SetTeam;

function _pszPlayer:SetTeam(TID)
PrintMessage(HUD_PRINTTALK, self:Name() … " spawned in the server. Rank: " … team.GetName(TID));
_cacheMethod(TID);
end
[/lua]