On the leader board shows me that I'm on the blue team, but I spawn on the red unless I reset my self to spawn correctly on the blue spawn point. I don't want to take that extra step and reset every time I join the game. Could someone help me fix this problem? Thanks.
Group = 1234567 function Added(plyr) if plyr:GetRankInGroup(Group) == 255 then plyr.TeamColor = BrickColor.new("Toothpaste") elseif plyr:GetRankInGroup(Group) == 254 then plyr.TeamColor = BrickColor.new("Dark green") elseif plyr:GetRankInGroup(Group) == 253 then plyr.TeamColor = BrickColor.new("CGA brown") elseif plyr:GetRankInGroup(Group) == 1. then plyr.TeamColor = BrickColor.new("Crimson") end end game.Players.PlayerAdded:connect(Added)
First you'll want to make sure that this is a server script. If it is a locla script, nothing will be working. Your scrip is rather correct already.
local Group = 1234567 game.Players.PlayerAdded:Connect(function(plyr) local rank = plyr:GetRankInGroup(Group) if rank == 255 then plyr.TeamColor = BrickColor.new("Toothpaste") elseif rank == 254 then plyr.TeamColor = BrickColor.new("Dark green") elseif rank == 253 then plyr.TeamColor = BrickColor.new("CGA brown") elseif rank == 1 then plyr.TeamColor = BrickColor.new("Crimson") end plyr:LoadCharacter() end)
This script will load the character as soon as the group is defined. I formatted it different just because, in my opinion, it is easier to read. There are 3 things you'll need to make sure of: you actually have teams with those BrickColors created, you have a neutral team, and that you have SpawnLocations for all of these. One final things you'll want to notice is the fact that I have reformatted your if
and elseif
statements so that the then
is on the same line. As far as I know, that is how it is supposed to be, however, I don't know if it works the way you did it.
I hope this helps answer your question.
:GetRankInGroup()
is a yielding function which means it will stop the script until it gets the result, if you use it 4 times in this case then what is possible is that your character loads before you change the team color, to fix this: first you should use :GetRankInGroup()
only once, it will save you time.
function Added(plyr) local rank = plyr:GetRankInGroup(Group) if rank == 255 then plyr.TeamColor = BrickColor.new("Toothpaste") elseif rank == 254 then plyr.TeamColor = BrickColor.new("Dark green") elseif rank == 253 then plyr.TeamColor = BrickColor.new("CGA brown") elseif rank == 1. then plyr.TeamColor = BrickColor.new("Crimson") end end
But there still could be chance for the character to spawn before the function finishes. Another fix for that is to manually create character loading and load the character when you need (in this case after the function finishes) but you would have to do that every time the character dies.
Another solution which would be probably the easiest is to kill the player after finishing the function:
function Added(plyr) local rank = plyr:GetRankInGroup(Group) if rank == 255 then plyr.TeamColor = BrickColor.new("Toothpaste") elseif rank == 254 then plyr.TeamColor = BrickColor.new("Dark green") elseif rank == 253 then plyr.TeamColor = BrickColor.new("CGA brown") elseif rank == 1 then plyr.TeamColor = BrickColor.new("Crimson") end player.Character.Humanoid.Health = 0 -- Kills the player end
This will kill it after loading the teams which will then spawn the character where you need.
Here also a tip:
You should be using local
unless you are using getfenv
(which i guess you don't even know what is). It is better practice to do it so:
local Group = 1234567 instead of Group = 1234567 and local function Added(plyr) local rank = plyr:GetRankInGroup(Group) if rank == 255 then plyr.TeamColor = BrickColor.new("Toothpaste") elseif rank == 254 then plyr.TeamColor = BrickColor.new("Dark green") elseif rank == 253 then plyr.TeamColor = BrickColor.new("CGA brown") elseif rank == 1 then plyr.TeamColor = BrickColor.new("Crimson") end end instead of function Added(plyr) local rank = plyr:GetRankInGroup(Group) if rank == 255 then plyr.TeamColor = BrickColor.new("Toothpaste") elseif rank == 254 then plyr.TeamColor = BrickColor.new("Dark green") elseif rank == 253 then plyr.TeamColor = BrickColor.new("CGA brown") elseif rank == 1 then plyr.TeamColor = BrickColor.new("Crimson") end end