How to skip waiting for a function to finish before running the rest of the code?

So I am making a game with the concept of don’t be crushed by a speeding wall with my own twist, but I’ve run into an issue. So in the game there are multiple tunnels where the walls will be, and I am trying to make it so that every few seconds, the walls come flying down the corridor. The issue is that instead of all the walls coming down at once, one wall moves all the way down then comes back, then the next wall does the same. Basically, instead of doing it all at the same time, the do it one by one.

here is the server script that activates them:

local JimmyModule = require(game.ServerScriptService.JimmyModule)
local ActivateJimmy = game.ReplicatedStorage.Remotes.ActivateJimmy

while wait(10) do
	ActivateJimmy:FireAllClients(true, false, false, nil)
	for i,v in pairs(game.Workspace.JimmyStuff.Jimmys:GetChildren()) do
		JimmyModule.MoveJimmy(v)		
	end
end

and here is the Jimmy Module that the server script requires:

local JimmyModule = {}

local TS = game:GetService("TweenService")

local CloseInfo = TweenInfo.new(
10, -- length
Enum.EasingStyle.Linear, --EasingStyle
Enum.EasingDirection.Out, -- Easing Direction
0, -- How many times the tween will repeat
false, -- Should the tween repeat?
0 -- Delay between each tween
)

function MoveJimmy(Jimmy, JimmyStart, JimmyEnd, OpenTime)
       local openProperties = {
		Position = JimmyEnd
	}
	local closeProperties = {
		Position = JimmyStart
	}
	
	local OpenInfo = TweenInfo.new(
		OpenTime, -- length
		Enum.EasingStyle.Linear, --EasingStyle
		Enum.EasingDirection.Out, -- Easing Direction
		0, -- How many times the tween will repeat
		false, -- Should the tween repeat?
		0 -- Delay between each tween
	)
	
	local OpenTween = TS:Create(Jimmy, OpenInfo, openProperties)
	local CloseTween = TS:Create(Jimmy, CloseInfo, closeProperties)

	Jimmy.BrickColor = BrickColor.new("Bright red")
	if Jimmy:FindFirstChild("Happy") and Jimmy:FindFirstChild("Angry")then
		Jimmy.Happy.Transparency = 1
		Jimmy.Angry.Transparency = 0
	end

	OpenTween:Play()
	wait(OpenTime)
	wait(1)
	CloseTween:Play()
	wait(10)
	
	Jimmy.BrickColor = BrickColor.new("Lime green")
	if Jimmy:FindFirstChild("Happy") and Jimmy:FindFirstChild("Angry")then
		Jimmy.Happy.Transparency = 0
		Jimmy.Angry.Transparency = 1
	end
end

function JimmyModule.MoveJimmy(Jimmy)
	local JimmyStart = game.Workspace.JimmyStuff.Start:FindFirstChild(Jimmy.Name.."Start").Position
	local JimmyEnd = game.Workspace.JimmyStuff.End:FindFirstChild(Jimmy.Name.."End").Position
	
	local JimmyDistance = (Jimmy.Position - JimmyEnd).Magnitude / 28.15
	print(JimmyDistance)
	
	MoveJimmy(Jimmy, JimmyStart, JimmyEnd, JimmyDistance)
end

return JimmyModule

I looked at some other topics and they said to use coroutines but I’m not sure how to use them and when I put them in the server script, whatever was wrapped in it just didn’t run. Thank you if you can help!

You need to use coroutines or the spawn & delay function. They let you run multiple while loops, loops in one script at the same time

1 Like

Hey yeah, you can use the delay function which is used to delay a function but it won’t yield the code like example:

local function printSomething()
    print("Hi")
end

delay(2, printSomething) -- 1st parameter is the time you wanna delay the function and second one is the function.

I tried this but it seems to be doing the exact same thing where it just does it one by one.

Here is my script now:

local JimmyModule = require(game.ServerScriptService.JimmyModule)
local ActivateJimmy = game.ReplicatedStorage.Remotes.ActivateJimmy

local function FireCorridorJimmys()
	for i,v in pairs(game.Workspace.JimmyStuff.Jimmys:GetChildren()) do
		JimmyModule.MoveJimmy(v)		
	end
end

while wait(10) do
	ActivateJimmy:FireAllClients(true, false, false, nil)
	
	
	delay(0, FireCorridorJimmys)
end

I think it’s not working because you’re firing the function once every 10 seconds so only one moves at a time.

I’m not a scripting expert

So I changed around the script and its working now, I put the delay on the wrong part. Thanks for the help!