Collection Service touched events aren't working

Hello dear developers. I’m making a game which you can make your own place with parts, lavas, buttons, even more. You can build anything you want.

However, today i ran to a problem with Collection Service using touched event. I’m trying to use Collection Service for multiple lava parts. The lavas can be placed by players.
After placing the lava part the collection service won’t listen touched event to that part. Only listens while it’s placed in workspace.

I tried using debugging, it works fine expect for the touched event. I checked for CanTouch option, still nothing seems to be working.

I tried looking for developer forums. Also tried getting help from the ChatGPT. But there is nothing solved my problem yet. Even ChatGPT could’nt help me.

Here is my code.
SERVER SCRIPT INSIDE SERVERSCRIPTSERVICE

--> Services <--

CollectionService = game:GetService("CollectionService")



--> Tags <--

Lavas = CollectionService:GetTagged("Lava")



--> Local Functions <--

local function PatrolLavas()
	print("function triggered")
	
	task.spawn(function()
		while task.wait(0.1) do --> Sometimes a part with another tag can be added to the Lava tag, so we need to update the table accordingly
			Lavas = CollectionService:GetTagged("Lava")
		end
	end)
	
	for index, lava in pairs(Lavas) do
		print("lava:", index)
		
		lava.Touched:Connect(function(Hit)
			print("lava touched")
			if Hit and Hit.Parent and Hit.Parent:FindFirstChildOfClass("Humanoid") then
				print("humanoid found")
				Hit.Parent:FindFirstChildOfClass("Humanoid"):TakeDamage(100)
			end
		end)
	end
end



--> Running Functions <--

print(Lavas)

PatrolLavas()

Here are some screenshoots of the issue:
image

image

image

image

2 Likes

You should probably store the connections in a table and disconnect them when required.

See the “Deadly bricks using Collection Service” here:

CollectionService | Documentation - Roblox Creator Hub

1 Like

I changed your code to run the function and update the table every time a descendant gets added to workspace:

--> Services <--

CollectionService = game:GetService("CollectionService")



--> Tags <--

Lavas = CollectionService:GetTagged("Lava")

--> Local Functions <--

local function PatrolLavas()
	print("function triggered")
	for index, lava in pairs(Lavas) do
		print("lava:", index)

		lava.Touched:Connect(function(Hit)
			print("lava touched")
			if Hit and Hit.Parent and Hit.Parent:FindFirstChildOfClass("Humanoid") then
				print("humanoid found")
				Hit.Parent:FindFirstChildOfClass("Humanoid"):TakeDamage(100)
			end
		end)
	end
end



--> Running Functions <--

print(Lavas)

-- Update lava table and rerun function every time a descendant gets added to workspace.
workspace.DescendantAdded:Connect(function()
	Lavas = CollectionService:GetTagged("Lava")
	PatrolLavas()
end)

What was happening was is that the function only loops trough the original lava table, so my idea was to run it every time a descendant was added. It’s working perfectly for me!

why would you do that?

why not just use GetInstanceAddedSignal its way better then looping through every object in workspace

It’s all i could think off to fix it.

1 Like
--> Services <--

CollectionService = game:GetService("CollectionService")



--> Tags <--

Lavas = CollectionService:GetTagged("Lava")



--> Local Functions <--

local function PatrolLavas()
	print("function triggered")
	
	task.spawn(function()
		while task.wait(0.1) do --> Sometimes a part with another tag can be added to the Lava tag, so we need to update the table accordingly
			Lavas = CollectionService:GetTagged("Lava") -- lava updates properly
		end
	end)
	
	for index, lava in pairs(Lavas) do -- this will only work for the old lava list.
		print("lava:", index)
		
		lava.Touched:Connect(function(Hit)
			print("lava touched")
			if Hit and Hit.Parent and Hit.Parent:FindFirstChildOfClass("Humanoid") then
				print("humanoid found")
				Hit.Parent:FindFirstChildOfClass("Humanoid"):TakeDamage(100)
			end
		end)
	end
end



--> Running Functions <--

print(Lavas)

PatrolLavas()

This is because when you update the lava table it doesn’t update the for loop. You need to use another method. For example:
workspace:GetPartsInPart(BasePart):{BasePart}` which returns a table of baseparts which you can loop through and see if a humanoid was touched. Do this in the while loop.

Sorry for replying really late. But now i will try you guys suggestions. Thank you for helping me.

This is actually what i might need. Because the script doesn’t updates the listeners for the new added lavas.

Your code actually worked. Thank you.

I actually forgot that i had to update the event listeners too. Thank you for noticing me.

Thank you whoever helped me fixing the problem. Have good day everyone :smile: