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

Wait after click?

Asked by
k1nq 30
7 years ago

So, I was wondering, how could I make this script wait until it's done running for me to be able to click again?

function onClicked(player)  
    local item = game.ServerStorage.Items.Logs:Clone()
    local gui = player.PlayerGui.ScreenGui.MainGui.Guis.Backpack
    local Number = script.Number.Value
    local Run = script.Value.Value
    Run = true
    while Run == true do
        if gui["B"..Number].Used.Value == false then
            item.Parent = gui["B"..Number]
            gui["B"..Number].Used.Value = true
            gui["B"..Number].Image = item.ItemPic.Value
            gui["B"..Number].Script.Disabled = false
            Number = 1
            Run = false
            script.Parent.Model.Root:Destroy()
            wait(10)
            local tree = game.ServerStorage.Items.Tree:Clone()
            tree.Parent = game.Workspace
            script.Parent:Destroy()
        else
            wait()
            Number = Number + 1
        end
    end
end

script.Parent.Model.Root.ClickDetector.MouseClick:connect(onClicked)

2 answers

Log in to vote
2
Answered by 7 years ago

This can be easily accomplished using debounce.

Let's add debounce to your script;

debounce = false --Declare what debounce is

function onClicked(player)  
    if debounce == true then return end --if debounce is on, then it will halt, if not then continue 
    debounce = true --Now debounce is true
    local item = game.ServerStorage.Items.Logs:Clone()
    local gui = player.PlayerGui.ScreenGui.MainGui.Guis.Backpack
    local Number = script.Number.Value
    local Run = script.Value.Value
    Run = true
    while Run == true do
        if gui["B"..Number].Used.Value == false then
            item.Parent = gui["B"..Number]
            gui["B"..Number].Used.Value = true
            gui["B"..Number].Image = item.ItemPic.Value
            gui["B"..Number].Script.Disabled = false
            Number = 1
            Run = false
            script.Parent.Model.Root:Destroy()
            wait(10) -- Since there is a wait here, it will take 10 seconds before it is able to be fired again
            local tree = game.ServerStorage.Items.Tree:Clone()
            tree.Parent = game.Workspace
            script.Parent:Destroy()
        else
            wait()
            Number = Number + 1
        end
    end
    debounce = false -- Now debounce is false, and the script can run again
end

script.Parent.Model.Root.ClickDetector.MouseClick:connect(onClicked)

Hope this helps!! If something is wrong, please comment and I will help you further!

Ad
Log in to vote
0
Answered by 7 years ago

ummm, i'm not a script expert but, Debounce really helps a lot

local variablesYouGot

debounce = false

local function Function()
    if debounce == false then

        debounce = true

        --stuff you need to do

        debounce = false
    end
end

StuffEvents:connect(Function)

done, hope this helps ;)

Answer this question