Tags Not Working

Hello, I have a script that uses tags for the checkpoints it works when a part outside of the map folder is given the tag but in the map folder it doesnt work

local cs = game:GetService("CollectionService")
local remoteEvent = game.ReplicatedStorage.FakeRespawnPlayer

local electricSound = script.ShockLoop

for _, part in cs:GetTagged("resetBrick") do
	part.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			print("Damage hit")
			hit.Parent.HumanoidRootPart.Position = game.Players:GetPlayerFromCharacter(hit.Parent):GetAttribute("CurrentCheckpoint")
		end
	end)
	
		local soundClone = electricSound:Clone()
		soundClone.Parent = part
		soundClone:Play()
		print("Playing Sound")
end

for _, part in cs:GetTagged("checkpoint") do
	part.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			part.Color = Color3.new(0, 1, 0.333333)
				-- Saves a vector3 value
			game.Players:GetPlayerFromCharacter(hit.Parent):SetAttribute("CurrentCheckpoint", part.Position)
			print("Checkpoint Saved")
		end
end)
end

Now its not doing anything i’ve tried doing print statements but its not printing anything also when I insert a part that and give it the tag it works as intended. Thanks for any help.

3 Likes

The tags are streaming in after your code runs. Make sure to also listen to CollectionService:GetInstanceAddedSignal

1 Like

So should I wrap this whole code in that function? So when the instance is added it loops through the instances again?

1 Like
local cs = game:GetService("CollectionService")
local remoteEvent = game.ReplicatedStorage.FakeRespawnPlayer

local electricSound = script.ShockLoop

cs:GetInstanceAddedSignal("checkpoint"):Connect(function()


for _, part in cs:GetTagged("checkpoint") do
	part.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			part.Color = Color3.new(0, 1, 0.333333)
				-- Saves a vector3 value
			game.Players:GetPlayerFromCharacter(hit.Parent):SetAttribute("CurrentCheckpoint", part.Position)
			print("Checkpoint Saved")
		end
end)
end
end)

cs:GetInstanceAddedSignal("resetBrick"):Connect(function()
	for _, part in cs:GetTagged("resetBrick") do
	part.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			print("Damage hit")
			hit.Parent.HumanoidRootPart.Position = game.Players:GetPlayerFromCharacter(hit.Parent):GetAttribute("CurrentCheckpoint")
		end
	end)
	
		local soundClone = electricSound:Clone()
		soundClone.Parent = part
		soundClone:Play()
		print("Playing Sound")
	end
end)

1 Like