How would i make a gui appear if a players on a team

Okay so uhh im not exactly trying to have a gui appear if a player’s on a team because i already have that script but im trying to make it so. A gui appears if a players on a team “Team2” but make a different one appear if hes on team “Team1” but it doesn’t work. Why?

game.Players.PlayerAdded:Connect(function(player)
local ClickDetector = script.Parent
	ClickDetector.MouseClick:Connect(function()
		for _, p in ipairs(game.Players:GetPlayers()) do
			if player.Team.Name == "Team2" then
				local PGui = p.PlayerGui
				PGui.ExplosionGui.Enabled = true
			else
				player.PlayerGui.DenyGui.Enabled = true
			end
		end
	end)
end)
1 Like

because you are looping all over every player in the game

you don’t need to do that when you already have the player from ClickDetector

additionally you can compare Player.Team to Team (instance instead)

you’re also not disabling the other UI so there might be an overlap

you should name your variables properly so they aren’t confusing

2 Likes

Alright so ur telling me to delete the first line “game.Players.PlayerAdded:Connect(function(player)” ?

1 Like

remove playeradded and the loop

2 Likes

Alrightyyyyyyyyyyyyyyyyyyyyyyyyyyyy

1 Like

One more thing. When i delete the playeradded i cannot refer to the teams anymore. So how would i fix that?

1 Like

ClickDetector.MouseClick passes the player who clicked so use that to get the player

ClickDetector.MouseClick:Connect(function(player: Player) end)

1 Like

i get Workspace.detonator.Button.ClickDetector.Script:2: attempt to call a RBXScriptSignal value

1 Like

sorry, forgot to add connect. it’s edited now

1 Like

Alright ill try the new version.

1 Like
local ClickDetector = script.Parent

ClickDetector.MouseClick:Connect(function(player)
    if player.Team.Name == "Team2" then
		player.PlayerGui.ExplosionGui.Enabled = true
	else
		player.PlayerGui.DenyGui.Enabled = true
	end
end)

you can try moving the function outside

2 Likes

I don’t understand why but the denyGui keeps showing up even though im on “Team2”

1 Like

try Disabling it when you Enable the other one so they don’t overlap due to both being enabled at the same time

1 Like

Is the gui on starter gui? Or there might be a code interfering

1 Like

yeah its on starter gui.,.,.,.,.

1 Like

Yeah uh, whenever a player spawns, everything from startergui gets copied to playergui i suggest you add the gui from a script if you dont want it to add as soon as the player join

1 Like
local ClickDetector = script.Parent

ClickDetector.MouseClick:Connect(function(player)
    if player.Team.Name == "Team2" then
		player.PlayerGui.ExplosionGui.Enabled = true
		player.PlayerGui.DenyGui.Enabled = false
	else
			player.PlayerGui.ExplosionGui.Enabled = false
		player.PlayerGui.DenyGui.Enabled = true
	end
end)

okay so uhh i made this script and as always the denyGUi gets enabled even though im on Test2 :+1:

1 Like

Have you checked if the team2 is actually named with capitals? Example : Team2 (with capitals)
team2 (without capitals)

1 Like

if player.Team.Name == "Team2" then

obraz

1 Like

Change Team2 to Test2, the name arent matching

1 Like