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

Can a Function be Called Again Before Completion?

Asked by 4 years ago

I am trying to make a gate rise, with each part only rising a certain amount. I want the function (raiseGate) to act on all parts of the gate at once rather than doing each piece one at a time. Is there a way to do this? Thanks so much!

function raiseGate(piece)
    local amount = piece.Order.Value
    for i = 0, amount do
        piece.CFrame = piece.CFrame + Vector3.new(0, 1.9629, 0)
        wait(.2)
    end
end

function onClicked()
    local gates = game.workspace.GateModel:GetChildren()
    for i, child in ipairs(gates) do
        raiseGate(child)
        wait(.1)
    end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

2 answers

Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

What I would do in this situation is use spawn.

You could use coroutines but I find spawns to be easier. Unlike coroutines, spawn does not return a reference.

Using spawn will create a new thread that will allow us to skip over the wait inside the raiseGate function.

So lets utilize it here

function raiseGate(piece)
    local amount = piece.Order.Value
    for i = 0, amount do
        piece.CFrame = piece.CFrame + Vector3.new(0, 1.9629, 0)
        wait(.2)
    end
end

function onClicked()
    local gates = game.workspace.GateModel:GetChildren()
    for i, child in ipairs(gates) do
    --here i create a new thread using spawn and places the raiseGate function there
    spawn(function()
            raiseGate(child)
    end)
    --Notice how I added the .2 from the raiseGate function to take into account how long raiseGate lasts
        wait(.1 + .2)
    end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

I added .2 to the wait outside the spawn function because the .2 of raiseGate is in another thread.

0
Technical note: "spawn" is a function that creates a coroutine/thread (you aren't "creating a spawn") chess123mate 5873 — 4y
0
You shouldn't create threads when the task can be easily done without them (like chess123mate's answer) because waits are not precise and the thread manager needs to waste processing power managing all your threads. hiimgoodpack 2009 — 4y
0
. hiimgoodpack 2009 — 4y
0
@hiimgoodpack the question was "Can a Function be CALLED AGAIN BEFORE Completion?" which I answered perfectly. though I wouldn't disagree with you either. DanzLua 2879 — 4y
0
What the OP wanted to actually do didn't need you to call a function again before it's done. hiimgoodpack 2009 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

You should increase the height of all parts before waiting. To ensure the loop doesn't start a second time before the first time has finished, use debounce: https://developer.roblox.com/articles/Debounce.

local db = false
function onClicked()
    if db then return end
    db = true
    local gates = game.Workspace.GateModel:GetChildren()
    for i = 0, amount do
        for i, piece in ipairs(gates) do
            local amount = piece.Order.Value
            piece.CFrame = piece.CFrame + Vector3.new(0, 1.9629, 0)
        end
        wait(.2)
    end
    db = false
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

Answer this question