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

How to tell a script to wait if its already running?

Asked by 8 years ago
Edited 8 years ago

I'm making a door and I want it to check if the script is already running, and if it is then it will wait until the function is complete before running another function. I noticed the return command could be used somehow, but I wasn't sure how to use it and if it was the best way to tell the script to wait. When I click the OnDoor button once it works but when I click it multiple times the script runs at the same time as the other ones which haven't completed.

ButtonOff script

Door = game.Workspace.TDoor.TheDoor
ButtonOn = game.Workspace.TDoor.ButtonOn

function OffDoor()
    Door.Transparency = 0
    Door.CanCollide = true
    ButtonOn.Material = Enum.Material.Plastic
end

script.Parent.ClickDetector.mouseClick:connect(OffDoor)

Button on script

Door = game.Workspace.TDoor.TheDoor
ButtonOn = game.Workspace.TDoor.ButtonOn

function OnDoor()
    Door.Transparency = 0.5
    Door.CanCollide = false
    wait(0.5)
    ButtonOn.Material = Enum.Material.Neon
    wait(0.5)
    ButtonOn.Material = Enum.Material.Plastic
    wait(0.5)
    ButtonOn.Material = Enum.Material.Neon
    wait(0.5)
    ButtonOn.Material = Enum.Material.Plastic
    wait(0.5)
    ButtonOn.Material = Enum.Material.Neon
    wait(0.5)
    ButtonOn.Material = Enum.Material.Plastic
    wait(0.5)
    ButtonOn.Material = Enum.Material.Neon
end

script.Parent.ClickDetector.mouseClick:connect(OnDoor)

1 answer

Log in to vote
0
Answered by 8 years ago

You're missing a Debounce which is the use of a variable to see if something is already being done so you don't spam the action.

The above link will teach you all about Debounces. Bellow I will show the solution to your problem.

ButtonOff

Door = game.Workspace.TDoor.TheDoor
ButtonOn = game.Workspace.TDoor.ButtonOn
IsRunning = false
function OffDoor()
    if IsRunning == false then
        IsRunning = true -- Stops above condition from being passed
        Door.Transparency = 0
        Door.CanCollide = true
        ButtonOn.Material = Enum.Material.Plastic
        IsRunning = false -- Allows above condition to be passed now
    end
end

script.Parent.ClickDetector.mouseClick:connect(OffDoor)

ButtonOn

Door = game.Workspace.TDoor.TheDoor
ButtonOn = game.Workspace.TDoor.ButtonOn
IsRunning = false
function OnDoor()
    if IsRunning == false then
        IsRunning = true
        Door.Transparency = 0.5
        Door.CanCollide = false
        wait(0.5)
        ButtonOn.Material = Enum.Material.Neon
        wait(0.5)
        ButtonOn.Material = Enum.Material.Plastic
        wait(0.5)
        ButtonOn.Material = Enum.Material.Neon
        wait(0.5)
        ButtonOn.Material = Enum.Material.Plastic
        wait(0.5)
        ButtonOn.Material = Enum.Material.Neon
        wait(0.5)
        ButtonOn.Material = Enum.Material.Plastic
        wait(0.5)
        ButtonOn.Material = Enum.Material.Neon
        IsRunning = false
    end
end

script.Parent.ClickDetector.mouseClick:connect(OnDoor)

If you add more wait after IsRunning becomes true, it will take longer for a player to be allowed to click the button again.

Ad

Answer this question