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

How to use debounce in this script?

Asked by 9 years ago
function Shutdown()
    local m = Instance.new ("Message")
    m.Parent = game.Workspace
    m.Text = "The owner is shutting down the server..."
    wait(3)
    game.Players:ClearAllChildren()
m:Destroy()
end

script.Parent.MouseButton1Click:connect(Shutdown)

This is a script in when you click the shutdown button, everyone gets banned from the game.

How do I add a debounce to prevent multiple messages coming in, and how do I make it so ALL the messages get deleted? (Because whenever the button is spammed, it makes multiple messages that don't go away.)

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

You want to have some variable marking whether or not you're allowed to show the message; call it canShutdown for example.

It needs to start so that you can press the button once and it will work:

local canShutdown = true

You want the messages to only appear when you're allowed to shut down:

local canShutdown = true

function Shutdown()
    if canShutdown then
        local m = Instance.new ("Message", workspace)
        -- Using second parameter of Instance.new makes it a little neater!
        m.Text = "The owner is shutting down the server..."
        wait(3)
        game.Players:ClearAllChildren()
        m:Destroy()
    end
end

script.Parent.MouseButton1Click:connect(Shutdown)

And you want to make it so that you can't shut down while there is a message already:

local canShutdown = true

function Shutdown()
    if canShutdown then
        canShutdown = false -- No longer can make new ones

        local m = Instance.new ("Message", workspace)
        m.Text = "The owner is shutting down the server..."
        wait(3)
        game.Players:ClearAllChildren()
        m:Destroy()

        canShutdown = true
        -- Now we can press it again, since old message is gone
    end
end

script.Parent.MouseButton1Click:connect(Shutdown)
Ad

Answer this question