Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I add a cooldown to the sound in my code?

Asked by 3 years ago

For my horror game, I've made a moving door with a sound that is connected with a button, but the sound plays again every time you click the button with no cooldown. I have tried many ways to add a cooldown but none of them work for me.

local ClickDetector = script.Parent.ClickDetector

local Door = script.Parent.Parent.Door

local TweenService = game:GetService("TweenService")

local Info = TweenInfo.new(

    2,
    Enum.EasingStyle.Back,
    Enum.EasingDirection.Out,
    0,
    true,
    0
)           

local Goal =

    {
        Position = Vector3.new(Door.Position.X - 4, Door.Position.Y - 0.5,Door.Position.Z - 1.7)
    }

local MoveDoor = TweenService:Create(Door, Info, Goal)

ClickDetector.MouseClick:Connect(function()

    MoveDoor:Play()
    script.RustyDoor:Play()

end)

1 answer

Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago

You should use debug/ debounces.

Also, debounces does not add a cool-down to your script, you need to add a wait inside:

Example:

debounce = false

script.Parent.Touched:Connect(function(player)
    if debounce == false then
        debounce = true
        print(player.Parent.Name .." touched me!"
        wait(1)
        debounce = false
    else return end
end)

This will not spam the message "GamingMadhi touched me!", but waits a second and detects if your touching and launch again.

Ad

Answer this question