How do i detect when a player switches to a diffrent team to fire a script?

ok, where is the local script located?

1 Like

Inside a gui
image

can u send a video of it with the output in it?

1 Like

Like the output of the script?

like a video of u changing ur team and make sure that the output is visible there

1 Like

I can’t really help you, my code works just fine(from my testing it detects changes). It just seems an issue on your end rather than mine.

local Teams = game:GetService("Teams")
plr:GetPropertyChangedSignal("Team"):Connect(function()
print(plr.Team.Name)
-- If you wanna check if a player has joined a specific team then
-- If plr.Team == Teams.TEAMNAME then
-- print("Player has enterred a specific team")
-- end
-- If you wanna check if a player has went neutral then
-- If plr.Neutral == true then
-- print("Player has went neutral")
--end
end)

EDIT: DO NOT PRINT TEAM.NAME that will error you

1 Like

@spydercam500 was this your solution?

1 Like

It seems that @Kaid3n22 's script worked though your script seems to be basically the same so yes i guess. But @Kaid3n22 said the script first so sorry lol

1 Like

One last question how do i make the team detector have a debounce so it cant fire again?

Yet again everyone forgets about the existence of the PlayerAdded RBXScriptSignal object (event) for team instances.

https://developer.roblox.com/en-us/api-reference/event/Team/PlayerAdded

local players = game:GetService("Players")

local teams = game:GetService("Teams")
local team = teams.Team --Example team instance.

team.PlayerAdded:Connect(function(player)
	print(player.Name.." joined "..team.Name..".")
end)

players.PlayerAdded:Connect(function(player)
	player.Team = team
end)

Similarly, the PlayerRemoved event is fired whenever a player is removed from/leaves a team instance.

https://developer.roblox.com/en-us/api-reference/event/Team/PlayerRemoved

1 Like

Will this work in a local script and will it only fire once?

team.PlayerAdded works in local scripts.

1 Like

Also where does my code go exactly?

Make sure the script is global? Because it would go really well in ServerScriptService

Unless it’s not global then i dont know

It cant the script is a local script and its because theres guis involved in the script.

Like I said, I don’t really know

Sorry for the late response, I was doing something. But to add a debounce so it can’t fire again, simply change a value to true and check whether it is false in the script.

local plr = game.Players.LocalPlayer
local debounce = false

plr:GetPropertyChangedSignal("Team"):Connect(function()
	if plr.Team and plr.Team.Name == "Team Name Here" and not debounce then
		debounce = true
		--do stuff
	end
end)
local plr = game.Players.LocalPlayer
local debounce = false
local wantedteam = --team path ex: game.Teams.Team

plr:GetPropertyChangedSignal("Team"):Connect(function()
	if plr.Team == wantedteam and not debounce then
		debounce = true
		--do stuff
	end
end)

I also fixed the team name one so that it won’t error if the team is nil.

1 Like