I have some teams in my game. If the player is in the 'Police' team, it should give the player some default tools.
What did I try?
Well, I tried this in a normal script inside ServerScriptService:
game.Players.PlayerAdded:Connect(function(Player) print(Player.TeamColor) if Player.TeamColor == "Really blue" then for i, tool in pairs(game.ReplicatedStorage.Tools:GetChildren()) do tool:Clone().Parent = Player.Backpack tool:Clone().Parent = Player.StarterGear end else print("Player is not in police team.") end end)
The colour 'Really blue' is for the Police Team. As you can see, I even tried to print the player's team colour. The output showed 'Really blue'. Which means the player is in the blue team. But when, inside the if statement, it does not recognize the team color. What I mean by saying is, that the else statement get's run instead of the if statement. If the team colour is 'Really blue', then it basically means that the player is in the police team. But then why is the else part running despite the output showing that the player is in the blue team. (NOTE: I have spelled everything correctly and the name of the teams is correctly written).
I even tried the above script in a characterAdded event but with no success.
Anyone have any idea what is causing the issues?
Thanks for any help
Good debugging! The problem is with your if
statement's comparison: if Player.TeamColor == "Really blue" then
. In times like this, when I don't understand what's going on, sometimes I also add print(Player.TeamColor, "Really blue", Player.TeamColor == "Really blue")
, though that wouldn't help in this case - but the problem becomes clear if you say print(typeof(Player.TeamColor), typeof("Really blue"))
, where the output will be BrickColor string
-- those are clearly not equal, which is why this code isn't behaving how you expect!
You can fix this by making it if Player.TeamColor.Name == "Really blue"
. If you have a Police "Team" in the Teams object, you could also add local policeTeam = game.Teams.Police
as the first line of the script (or use whatever the name is -- if it's "Police Officers" then use local policeTeam = game.Teams["Police Officers"]
and then the if
statement can be if Player.Team == policeTeam then
. The first option is better if you might change the name of the team, but the second option is better if you might change the team colour.
Hello, this is not my script but does work with my game. Make sure to put all of your tools in the team that you want to have them. Nothing is required to activate this script other than to put the script in the Workspace. Have a good day!
--Just put weapons in the team. function teamFromColor(color) for _,t in pairs(game:GetService("Teams"):GetChildren()) do if t.TeamColor==color then return t end end return nil end function onSpawned(plr) local tools = teamFromColor(plr.TeamColor):GetChildren() for _,c in pairs(tools) do c:Clone().Parent = plr.Backpack end end function onChanged(prop,plr) if prop=="Character" then onSpawned(plr) end end function onAdded(plr) plr.Changed:connect(function(prop) onChanged(prop,plr) end) end game.Players.PlayerAdded:connect(onAdded)