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()
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!
--> 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.