Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with Folder cloning a bunch of times?

local GlassFolder = game.ReplicatedStorage.FallingGlass1
local Touched = false

function BreakGlass()
    script.Parent.Transparency = 1
    script.Parent.Break:Play()
print'a' --This prints about 6 times every 5 seconds, should only print once though.
	local ClonedGlass = GlassFolder:Clone() --Same here, it clones about 6 times.
	ClonedGlass.Parent = script.Parent
	
    for i,v in pairs(ClonedGlass:GetChildren()) do
        v.Transparency = .6
        v.Anchored = false
    end
end

function RepairGlass()
    script.Parent.Transparency = 0
    local ClonedGlass = GlassFolder:Clone()
	ClonedGlass.Parent = script.Parent
end

script.Parent.Touched:connect(function(hit)
    if (hit.Parent.Name == 'Knife' or hit.Name == 'Bullet') and (not Touched)  then
		while wait(.1) do
	        Touched = true
	        BreakGlass()
	        wait(5)
			RepairGlass()
	        Touched = false
		end
    end
end)

1 answer

✓ Accepted answer

You're dramatically misusing the debounce pattern.

Here is how your event should look:

script.Parent.Touched:connect(function(hit)
	if Touched then
		return
	end
	if hit.Parent.Name == 'Knife' or hit.Name == 'Bullet' then
		Touched = true
		BreakGlass()
		wait(5)
		RepairGlass()
		Touched = false
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.