Enable or disable GUI depending on player's team

I’m not sure what I’m doing wrong

This is a script in StarterPlayerScripts which was intended to enable/disable a certain GUI depending on the player’s team

local player = game.Players.LocalPlayer
local teamgui = game.StarterGui.TeamGUI

player.Team.Changed:Connect(function()
	if player.Team == "Inmates" or "Police" then teamgui.Enabled = false elseif
		player.Team == "Choosing" then teamgui.Enabled = true
	end
end)
1 Like

When pointing to the GUI of an individual Player, you should point to PlayerGUI.

StarterGUI basically clones itself and places the cloned GUIs under each Player’s PlayerGUI. Just try changing the teamgui variable.

Edit:

You should use GetPropertyChangedSignal instead, as it specifies when a specific property in a Team changes.

player:GetPropertyChangedSignal("TeamColor"):Connect(function()
-- Code here!
end
2 Likes

StarterGui is just a container that copies all UI from there to player.PlayerGui everytime the player spawns, if you change the StarterGui UI properties instead of PlayerGui they won’t replicate to that specific player, but will replicate to all the players in the server after they respawn, if the UI has ResetOnSpawn set to true. Instead PlayerGui should be used.

I think this returns when the properties of the team the player currently is(when this line of code runs) change, not when the team itself changes. Instead you should use GetPropertyChangedSignal for this.

This statement isn’t equal to player.Team == "Inmates" or player.Team == "Police" but breaks down to player.Team == "Inmates" which is true if their team is Inmates and "Police" that is always true because lua logic translates strings to true by default, so the entire statement becomes true no matter the player team.
Also, another issue is that if the player team is set before starting to listen to changes, the GUI won’t be affected, so the code must run once before that.

With those into consideration, this is what you’re looking for:

--keep in mind, scripts in StarterPlayerScripts only run once at player join
--because of that I also modified the script to listen for when that UI gets added to the player
local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")

local UI: ScreenGui? = nil

function checkTeam()
	if not UI then return end
	if player.Team.Name == "Inmates" or player.Team.Name == "Police" then
		UI.Enabled = false
	elseif player.Team.Name == "Choosing" then
		UI.Enabled = true
	end
end

function instanceAdded(ui: Instance)
	if ui.Name ~= "TeamGUI" or not ui:IsA("ScreenGui") then return end
	UI = ui
	checkTeam()
end

playerGui.ChildAdded:Connect(instanceAdded)
for _, v in pairs(playerGui:GetChildren()) do instanceAdded(v) end

checkTeam()
player:GetPropertyChangedSignal("Team"):Connect(checkTeam)

Also, it must be a LocalScript.