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

Wait after click?

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)

1 comment

2 answers

✓ Accepted answer

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!

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 ;)

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