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

Wondering how to do this ive tried to do it but i cant get it to work.

I tried to use

local plr = game.Players.LocalPlayer
local team = plr.Team

repeat task.wait(0.1) until team.Name == "Team name here"

-- code i want to fire goes here
end

1 Like

u could do

game.Players.PlayerAdded:Connect(function(plr)
	plr.Changed:Connect(function(p)
		if p == "Team" then
			local current_team = plr.Team
			if current_team then
				if current_team.Name == "TEAMNAMEHERE" then
					-- code here
				end
			end
		end
	end)
end)

OR you could do this for local player

local plr = game.Players.LocalPlayer

plr.Changed:Connect(function(p)
	if p:lower() == "team" then
		local current_team = plr.Team
		if current_team then
			if current_team.Name == "TEAMNAMEHERE"  then
			 	-- code here
			end
		end
	end
end)
1 Like

while @TheH0meLands solution will work, to produce less work for the server, I would use :GetPropertyChangedSignal()

local plr = game.Players.LocalPlayer

plr:GetPropertyChangedSignal("Team"):Connect(function()
	if plr.Team.Name == "Team Name Here" then
		--do stuff
	end
end)

you could also just use the team itself.

local plr = game.Players.LocalPlayer
local wantedteam = --team path ex: game.Teams.Team

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

Also, make sure to give the solution to @TheH0meLands as his answer does work and does do what you wanted.

2 Likes

one thing that’s a bit nitpicking, the team could possibly be nil when it’s changes so you should add an if statement to check if the team instance is valid. Still good script nonetheless :+1:

2 Likes

It will work whether the team is nil or not, its like

if nil == 0 then
	print("0")
else
	print("something else")
end

it won’t run and also doesn’t produce errors.

1 Like

oh nice, didn’t know that. I thought if you do something like nil.Name it’ll error. I guess not.

1 Like

oh, you meant that part, yes, it will error, but I assume that it wouldn’t be possible for them to have a nil team unless a script sets it to nil.

1 Like

Didnt actually appear to work surprisingly

his won’t work within a local script, his is meant to be used in a server script.

1 Like

use the seconds code sample I have for local scripts.

1 Like

also can you bring back the solution? :frowning:

1 Like

I used the local script version and it still didnt work.

send code

1 Like
plr.Changed:Connect(function(p)
if p:lower() == "team" then
	if team then
		if team.Name == "Nightmare" then
				detectionbox.Touched:Connect(function()
					local debounce = false
					if debounce == false then
						debounce = true
						for i = 1, #objectivecomplete do
							objectives.Text = string.sub(objectivecomplete,1,i)
							task.wait(Time)
						end
						local clocktween2 = ts:Create(game.Lighting,TweenInfo.new(),{["ClockTime"] = 0})
						clocktween2:Play()
						for i = 1, #story7 do
							story.Text = string.sub(story7,1,i)
							task.wait(Time)
						end
						task.wait(4)
						for i = 1, #story8 do
							story.Text = string.sub(story8,1,i)
							task.wait(Time)
						end
						task.wait(2)
						for i = 1, #objective3 do
							objectives.Text = string.sub(objective3,1,i)
							task.wait(Time)
						end
					end
				end)
			end
		end
	end
end)

try adding prints here

plr.Changed:Connect(function(p)
if p:lower() == "team" then
	if team then
		if team.Name == "Nightmare" then
print('team changed!')
				detectionbox.Touched:Connect(function()
					local debounce = false
					if debounce == false then
						debounce = true
						for i = 1, #objectivecomplete do
							objectives.Text = string.sub(objectivecomplete,1,i)
							task.wait(Time)
						end
						local clocktween2 = ts:Create(game.Lighting,TweenInfo.new(),{["ClockTime"] = 0})
						clocktween2:Play()
						for i = 1, #story7 do
							story.Text = string.sub(story7,1,i)
							task.wait(Time)
						end
						task.wait(4)
						for i = 1, #story8 do
							story.Text = string.sub(story8,1,i)
							task.wait(Time)
						end
						task.wait(2)
						for i = 1, #objective3 do
							objectives.Text = string.sub(objective3,1,i)
							task.wait(Time)
						end
					end
				end)
			end
		end
	end
end)

does it print? also my code sample works perfectly fine for me

1 Like

Doesnt print for some reason. (30)

are you sure you put the correct team name? It’s case sensitive.

Send me a screenshot of ur team explorer

1 Like

image

are you changing the player’s team at all?Like via a script or what?

1 Like

Its changing via a spawn point which is the detection box in the script.