Trouble using collection service to make killparts

So, I was trying to use collection service for making functional kill parts, however, I had a while loop in my code, which made it lag extremely badly. I was told not only that the while loop was useless, but that the script would function without it. I’ve seen tutorials in which they don’t include a while loop, and the kill parts in those work seamlessly. I really wanna know what I can do to make these “kill” parts functional. (It works with a while loop, but not without one)

-- Services --
local Collection_Service = game:GetService("CollectionService")

-- Core --
local Core = script.Parent:WaitForChild("Core")
local GR_Point = Core:WaitForChild("Respawn_Point")

function Reset(Humanoid_Root_Part, Respawn_Point)
	Humanoid_Root_Part.CFrame = Respawn_Point.CFrame
end



for I,V in pairs(Collection_Service:GetTagged("Kill_Part")) do
	V.Touched:Connect(function(Hit)
		print(GR_Point.Value.CFrame)
		if Hit.Parent:FindFirstChild("Humanoid") then
			local Humanoid_Root_Part = Hit.Parent:FindFirstChild("HumanoidRootPart")
			if Humanoid_Root_Part then
				Reset(Humanoid_Root_Part, GR_Point.Value)
			end
		end
	end)
end

1 Like

are you Tagging the kill parts at Runtime?
i.e. sometime where its possible that the bricks are getting the tags after your for loop?
(like on a separate script)

Thats the only issue i can see, as your code would only work for already existing tags. (those that are set by using the tag widget/window in studio).

This would also explain why it worked with a while loop.
Because otherwise you would have to listen for new parts that have received the tag. Like so:

Collection_Service:GetInstanceAddedSignal("Kill_Part"):Connect(function(part)
	part.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild('Humanoid') == nil then return end

		Hum_Root = hit.Parent:FindFirstChild('HumanoidRootPart')
		if Hum_Root == nil then return end

		Reset(Hum_Root,GR_Point.Value)
	end)
end)
1 Like

Thanks bro! I was looking at this function earlier, but I didn’t use it cause I thought it checked if a tag was created or something. The devhub made it seem like so

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.