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)
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.
You should increase the height of all parts before wait
ing. 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)