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

Why does this debounce SOMETIMES break this door?

I had put in a debounce feature into the door to prevent the Sound being spammed when the door "opens" (the door opens with a card tool- when players move their character from side to side with the card in their hand it spams the sound so I had used debounce as a cooldown)

However, after putting the debounce in, it only SOMETIMES works - sometimes it works perfectly, other times it acts completely broken and refuses to register. It will also happen in the same instance of playing - so I can open the door no problems, try to open it again 10 seconds later and it's just completely broken and refuses to work. Sometimes it will work up to 5 times before it breaks. Sometimes it just doesn't even work once.

Here is the script, I would love to hear what you guys think is the issue. It works perfectly without the debounce in there (well, you can still spam the sound which is not good, but at least it opens consistently every time)

Thank you so much!

print("Advanced SpecialDoor Script loaded")



permission = { "Fire Dorm" }
Door = script.Parent.Parent.PersonDoor
Door2 = script.Parent.Parent.PersonDoor2
DoorMesh = script.Parent.Parent.PersonDoor.Mesh
Sensor = script.Parent
Sounds = script.Parent.Parent.Sounds
debounce = false

function checkOkToLetIn(name)
    for i = 1,#permission do

        if (string.upper(name) == string.upper(permission[i])) then return true end
    end
    return false
end

function onTouched(hit)
    if debounce == false then
        debounce = true
        print("Sensor Hit")
        local human = hit.Parent:findFirstChild("Humanoid")
        if (human ~= nil ) then
            -- 
            print("Human touched door")

            if (checkOkToLetIn(human.Parent.Name)) then
                print("Human passed test")
                Wait(0.001)
                                script.Parent.Parent.Dot1.Transparency = 0
                                Sounds.Beep:Play()
                                wait(0.4)
                                script.Parent.Parent.Dot2.Transparency = 0
                                Sounds.Beep2:Play()
                wait(0.4) 
                                script.Parent.Parent.Dot3.Transparency = 0
                                Sounds.Beep2:Play()
                wait(0.4) 
                                script.Parent.Parent.Open.Transparency = 0
                                script.Parent.Parent.Close.Transparency = 1
                                Sounds.Beep2:Play()
                                wait(0.2)
                                Sounds.OpenSound1:Play()
                                wait(0.1)
                                script.Parent.Parent.HandlePart3.Transparency = 0
                                script.Parent.Parent.HandlePart4.Transparency = 0
                                script.Parent.Parent.HandlePart.Transparency = 1
                                script.Parent.Parent.HandlePart2.Transparency = 1
                                script.Parent.Parent.Handle2.Transparency = 1
                                script.Parent.Parent.Handle5.Transparency = 0
                                script.Parent.Parent.Acess.Mesh.Scale = Vector3.new(0.9, 0.5, 1.01)
                                Door.Transparency = 1
                                Door2.Transparency = 0
                                Door2.Wedge.Transparency = 0
                                Door2.Wedge2.Transparency = 0
                                Door.Wedge.Transparency = 1
                                Door.Wedge2.Transparency = 1
                                wait(0.02)
                Door.CanCollide = false
                                script.Parent.Parent.Dot1.Transparency = 1
                                script.Parent.Parent.Dot2.Transparency = 1
                                script.Parent.Parent.Dot3.Transparency = 1
                wait(3) 

                                script.Parent.Parent.Open.Transparency = 1
                                script.Parent.Parent.Close.Transparency = 0
                                script.Parent.Parent.HandlePart3.Transparency = 1
                                script.Parent.Parent.HandlePart4.Transparency = 1
                                script.Parent.Parent.HandlePart.Transparency = 0
                                script.Parent.Parent.HandlePart2.Transparency = 0
                                script.Parent.Parent.Handle2.Transparency = 0
                                script.Parent.Parent.Handle5.Transparency = 1
                                Sounds.OpenSound2:Play()
                                script.Parent.Parent.Acess.Mesh.Scale = Vector3.new(0.01, 0.01, 0.01)
                Door.CanCollide = true
                                Door.Transparency = 0 
                                Door2.Transparency = 1
                                Door2.Wedge.Transparency = 1
                                Door2.Wedge2.Transparency = 1
                                Door.Wedge.Transparency = 0
                                Door.Wedge2.Transparency = 0
                                wait(10)
                                debounce = false
            end
        end
    end

end

connection = Sensor.Touched:connect(onTouched) 

1 comment

0

That print on line 1 should be on the last line, lol

1 answer

The reason is because you set the debounce to true whenever the debounce is false, but only set the debounce back to false if what touched was a character model. This means if a character model wasn't the one that touched, the debounce would never get reset, and therefore nothing could touch the part that would make the code fire.

print("Advanced SpecialDoor Script loaded")



permission = { "Fire Dorm" }
Door = script.Parent.Parent.PersonDoor
Door2 = script.Parent.Parent.PersonDoor2
DoorMesh = script.Parent.Parent.PersonDoor.Mesh
Sensor = script.Parent
Sounds = script.Parent.Parent.Sounds
debounce = false

function checkOkToLetIn(name)
    for i = 1,#permission do

        if (string.upper(name) == string.upper(permission[i])) then return true end
    end
    return false
end

function onTouched(hit)
    if debounce == false then
        debounce = true
        print("Sensor Hit")
        local human = hit.Parent:findFirstChild("Humanoid")
        if (human ~= nil ) then
            -- 
            print("Human touched door")

            if (checkOkToLetIn(human.Parent.Name)) then
                print("Human passed test")
                Wait(0.001)
                                script.Parent.Parent.Dot1.Transparency = 0
                                Sounds.Beep:Play()
                                wait(0.4)
                                script.Parent.Parent.Dot2.Transparency = 0
                                Sounds.Beep2:Play()
                wait(0.4) 
                                script.Parent.Parent.Dot3.Transparency = 0
                                Sounds.Beep2:Play()
                wait(0.4) 
                                script.Parent.Parent.Open.Transparency = 0
                                script.Parent.Parent.Close.Transparency = 1
                                Sounds.Beep2:Play()
                                wait(0.2)
                                Sounds.OpenSound1:Play()
                                wait(0.1)
                                script.Parent.Parent.HandlePart3.Transparency = 0
                                script.Parent.Parent.HandlePart4.Transparency = 0
                                script.Parent.Parent.HandlePart.Transparency = 1
                                script.Parent.Parent.HandlePart2.Transparency = 1
                                script.Parent.Parent.Handle2.Transparency = 1
                                script.Parent.Parent.Handle5.Transparency = 0
                                script.Parent.Parent.Acess.Mesh.Scale = Vector3.new(0.9, 0.5, 1.01)
                                Door.Transparency = 1
                                Door2.Transparency = 0
                                Door2.Wedge.Transparency = 0
                                Door2.Wedge2.Transparency = 0
                                Door.Wedge.Transparency = 1
                                Door.Wedge2.Transparency = 1
                                wait(0.02)
                Door.CanCollide = false
                                script.Parent.Parent.Dot1.Transparency = 1
                                script.Parent.Parent.Dot2.Transparency = 1
                                script.Parent.Parent.Dot3.Transparency = 1
                wait(3) 

                                script.Parent.Parent.Open.Transparency = 1
                                script.Parent.Parent.Close.Transparency = 0
                                script.Parent.Parent.HandlePart3.Transparency = 1
                                script.Parent.Parent.HandlePart4.Transparency = 1
                                script.Parent.Parent.HandlePart.Transparency = 0
                                script.Parent.Parent.HandlePart2.Transparency = 0
                                script.Parent.Parent.Handle2.Transparency = 0
                                script.Parent.Parent.Handle5.Transparency = 1
                                Sounds.OpenSound2:Play()
                                script.Parent.Parent.Acess.Mesh.Scale = Vector3.new(0.01, 0.01, 0.01)
                Door.CanCollide = true
                                Door.Transparency = 0 
                                Door2.Transparency = 1
                                Door2.Wedge.Transparency = 1
                                Door2.Wedge2.Transparency = 1
                                Door.Wedge.Transparency = 0
                                Door.Wedge2.Transparency = 0
                            
            end
        end
    end
    wait(10) -- moved
    debounce = false
end

connection = Sensor.Touched:connect(onTouched) 

1 comment

0

Thank you for your response! That makes the door work again, and allows the sound to not be spammed the first time you use it, but after the first time it's back to spamming the door sound again. :( Thank you for your help!

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