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

It didnt seem to work as the code in the if statement still fires for some reason as seen here.

That’s weird, try adding a print statement to it.

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

Also, do you every set the debounce to false afterwards?

1 Like

It doesnt print it again but the touched event still fires? Even though its inside the GetPropertyChangedSignal??

can I see the current script with the code you added? It seems like there is some repeat when you are doing the text.

1 Like
plr:GetPropertyChangedSignal("Team"):Connect(function()
if  plr.Team and plr.Team.Name == "Nightmare" and not debounce1 then
	debounce1 = true
	print("team changed")
	detectionbox.Touched:Connect(function()
			local clocktween2 = ts:Create(game.Lighting,TweenInfo.new(),{["ClockTime"] = 0})
			clocktween2:Play()
			for i = 1, #objectivecomplete do
				objectives.Text = string.sub(objectivecomplete,1,i)
				task.wait(Time)
			end

			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)

Ah, now it makes more sense. It is running the touched event every time the player touches it because there is not a debounce for it.

plr:GetPropertyChangedSignal("Team"):Connect(function()
	if plr.Team and plr.Team.Name == "Nightmare" and not debounce1 then
		debounce1 = true
		local debounce2 = false
		detectionbox.Touched:Connect(function()
			if not debounce2 then
				debounce2 = true
				local clocktween2 = ts:Create(game.Lighting,TweenInfo.new(),{["ClockTime"] = 0})
				clocktween2:Play()
				for i = 1, #objectivecomplete do
					objectives.Text = string.sub(objectivecomplete,1,i)
					task.wait(Time)
				end

				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)
1 Like

Fixed thank you! (30aaaaaaaaa)