Spawning a part less than 2 times

Hello forum! i need help

while true do
	for _, part in pairs(PropertyTable) do
		part.Touched:Connect(function(hit)
			local debounce = false
			if hit.Parent:FindFirstChild("Humanoid") and part.Color == Color3.fromRGB(255, 0, 0) and PropertyTable and debounce == false then
				debounce = true
				hit.Parent.HumanoidRootPart.CFrame = JailIn.CFrame
				PlayerJail.Value += 1
				local ranX = math.random(-721, -639)
				local ranZ = math.random(700, 772)
				local ranY = 6.625
				
				local Hearth = game.ServerStorage.Hearth:Clone()
				Hearth.Parent = game:GetService("Workspace").SoulEndScript
				Hearth:PivotTo(CFrame.new(ranX, ranY, ranZ))
				task.wait(1)
				debounce = false
			end
		end)
	end
	task.wait(2)
end

clearly, the code is executing when we touch a part. However, even though i have implemented a debounce system on the script, it is generating more than one heart (over 100 of them) when we touch the part i want to create only one, how can i do that?

while true do
	for _, part in pairs(PropertyTable) do
        local debounce = false
		part.Touched:Connect(function(hit)
			
			if hit.Parent:FindFirstChild("Humanoid") and part.Color == Color3.fromRGB(255, 0, 0) and PropertyTable and debounce == false then
				debounce = true
				hit.Parent.HumanoidRootPart.CFrame = JailIn.CFrame
				PlayerJail.Value += 1
				local ranX = math.random(-721, -639)
				local ranZ = math.random(700, 772)
				local ranY = 6.625
				
				local Hearth = game.ServerStorage.Hearth:Clone()
				Hearth.Parent = game:GetService("Workspace").SoulEndScript
				Hearth:PivotTo(CFrame.new(ranX, ranY, ranZ))
				task.wait(1)
				debounce = false
			end
		end)
	end
	task.wait(2)
end
1 Like

If this doesn’t work try moving the debounce into the while loop and if that doesn’t work try moving it outside the loops

1 Like

I dont think this will work because every time you touch it will set debounce to false

1 Like

Oh yeah! youre right i dont know how i didnt notice that thx lol

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