• Get player team name in GM:PlayerInitialSpawn
    15 replies, posted
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: [CODE] function GM:PlayerInitialSpawn(ply) PlayerDataUpdate(ply) PrintMessage(HUD_PRINTTALK, ply:Name() .. " spawned in the server. Rank: " .. team.GetName(ply:Team())) end [/CODE] 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]
[QUOTE=Hyper Iguana;32849494]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][/QUOTE] I tried this but it still doesn't work. It just shows this: [IMG]http://img192.imageshack.us/img192/6700/joiningconnecting.png[/IMG] And the name of the team in which I am is Head Admin: [IMG]http://img171.imageshack.us/img171/2800/2011101900001.jpg[/IMG] 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.
[QUOTE=yoBrelliKX;32858212]Set the team first.[/QUOTE] 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?
[QUOTE=LuaMilkshake;32858242]How are you setting the player's team? Are you doing it in your own script, or are you using something like ULX?[/QUOTE] 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?
[QUOTE=LuaMilkshake;32858275] Are you actually using the UTeam script, or are you using the new team system built into ULX?[/QUOTE] 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.
[QUOTE=KingofBeast;32865140]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.[/QUOTE] OMG Thank you! :D
Why not find where it sets the players team and just do the printmessage loop there?
[QUOTE=shadowndacorner;32888874]Why not find where it sets the players team and just do the printmessage loop there?[/QUOTE] Because I didn't feel like going through every ULX lua file and looking for it?
[QUOTE=KingofBeast;32865140]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.[/QUOTE] 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]
Sorry, you need to Log In to post a reply to this thread.