The script doesn't fully work

The team changing is working perfectly, I want it either when player is Police then the CriminalLevelGui visibility will be false, and so when you’re a Criminal, PoliceLevelGui visibility will be false and CriminalLevelGui visibility will be true. How do I make ? I tried to figure this out for about 30 min but nothing seems to work.

The script:

local changeTeam = game:GetService("ReplicatedStorage").ChangeTeam
local player = game:GetService("Players").LocalPlayer

local menuButton

local teamBackround = script.Parent:WaitForChild("TeamBackround")

local policeColor = "Deep blue"
local CriminalColor = "Bright red"

teamBackround.CriminalTeamButton.MouseButton1Click:Connect(function()
	changeTeam:FireServer(BrickColor.new(CriminalColor))
end)

teamBackround.PoliceTeamButton.MouseButton1Click:Connect(function()
	changeTeam:FireServer(BrickColor.new(policeColor))
end)

if player.TeamColor == policeColor then
	script.Parent.Parent.CriminalLevelGUI.CriminalLevelGUIBackround.Visible = false
	script.Parent.Parent.PoliceLevelGUI.PoliceLevelGUIBackround.Visible = true
end

if player.TeamColor == CriminalColor then
	script.Parent.Parent.PoliceLevelGUI.PoliceLevelGUIBackround.Visible = false
	script.Parent.Parent.CriminalLevelGUI.CriminalLevelGUIBackround.Visible = true
end

instead of player.TeamColor use player.Team

local teams = game:GetService("Teams")
local policeTeam = teams.PoliceTeam

if player.Team == policeTeam then
    print('Player is a police.')
end
1 Like

So Player.TeamColor is a BrickColor and not just a string, so when you compare player.TeamColor == policeColor is should really be player.TeamColor == BrickColor.new(policeColor). Since everwhere you use policeColor and criminalColor needs them to be BrickColors, you can just do that when you define them.

You’re also never running the check for the player’s TeamColor when it changes. You can check for changes to the property with :GetPropertyChangedSignal(). Applying everything I said would look something like this:

local changeTeam = game:GetService("ReplicatedStorage").ChangeTeam
local player = game:GetService("Players").LocalPlayer

local teamBackground = script.Parent:WaitForChild("TeamBackround")

local policeColor = BrickColor.new("Deep blue")
local criminalColor = BrickColor.new("Bright red")

teamBackground.CriminalTeamButton.MouseButton1Click:Connect(function()
	changeTeam:FireServer(criminalColor)
end)

teamBackground.PoliceTeamButton.MouseButton1Click:Connect(function()
	changeTeam:FireServer(policeColor)
end)

local function updateLevelGuis()
	local isACriminal = player.TeamColor == criminalColor
	script.Parent.Parent.PoliceLevelGUI.PoliceLevelGUIBackround.Visible = not isACriminal
	script.Parent.Parent.CriminalLevelGUI.CriminalLevelGUIBackround.Visible = isACriminal
end

player:GetPropertyChangedSignal("TeamColor"):Connect(updateLevelGuis)
updateLevelGuis()

You don’t need to use Player.Team, but it definitely is more reliable in the long run. I say this because more than one team can use the same exact TeamColor. Not only that, but when you update the color for a team, if you’re relying on teams, you won’t need to worry about updating any of your code.

1 Like

I changed it to player.Team, it’s better as all of you said, and it is.

It still happens, when I change teams the Gui false and true still doesn’t work.

Here’s the ServerScript that is being communicated with the localScript:

game.ReplicatedStorage.ChangeTeam.OnServerEvent:Connect(function(player, team)
	player.Team = team
	player:LoadCharacter()
end)

You should probably do a little validation with that RemoteEvent on the server because malicious people could send fake data like nil as the team. You don’t need to do this, and it wouldn’t solve your current problem, but I thought I should mention it.

game.ReplicatedStorage.ChangeTeam.OnServerEvent:Connect(function(player, team)
	if typeof(team)=="Instance" and team:IsA("Team") then
		player.Team = team
		player:LoadCharacter()
	end
end)

On to your current problem, I’m guessing ResetOnSpawn is turned on for your PlayerGuis (PoliceLevelGUI & CriminalLevelGUI). Usually that property causes a lot headaches since it does exactly what the property says. You can try turning that property off and see if it fixes your problem.

1 Like

Thanks for this, it worked, loading player again was the problem.