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

Debounce not working? Help with sound not playing?

So I am wanting the sound to play one time but everytime my knife hits the "glass" the sound plays. Debounce is not working..

local GlassFolder = script.Parent.FallingGlass
local Touched = false

function BreakGlass()
	script.Parent.Transparency = 1
	script.Parent.Break:Play()
	for i,v in pairs(GlassFolder:GetChildren()) do
		v.Transparency = .6
		v.Anchored = false
	end
end

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

1 answer

✓ Accepted answer

This is why it's normally a good idea to enclose your conditionals in parenthesis. This will actually fix your problem.

local GlassFolder = script.Parent.FallingGlass
local Touched = false

function BreakGlass()
    script.Parent.Transparency = 1
    script.Parent.Break:Play()
    for i,v in pairs(GlassFolder:GetChildren()) do
        v.Transparency = .6
        v.Anchored = false
    end
end

script.Parent.Touched:connect(function(hit)
    if (hit.Parent.Name == 'Knife' or hit.Name == 'Bullet') and (not Touched)  then
        Touched = true
        BreakGlass()
        wait(1000000)
        Touched = false
    end
end)
I changed line 14 with parenthesis, and I used not because it's will do the same thing and it's shorter.

Good Luck!

If I helped, please don't forget to accept my answer :D

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