How can i make this code run fast

		repeat
			task.wait()
			print(CheckingPosition, xDelta, yDelta)
			
			
			if CheckingPosition.X ~= MaxPosition.X then
				CheckingPosition = Vector2.new(CheckingPosition.X + xDelta, CheckingPosition.Y)
				
			end
			
			if CheckingPosition.Y ~= MaxPosition.Y then
				CheckingPosition = Vector2.new(CheckingPosition.X, CheckingPosition.Y + yDelta)


			end
			
			table.insert(Moves, CheckingPosition)
			
		until CheckingPosition == MaxPosition

currently it runs slowly because of task.wait() but if it wasnt there it’d crash
i’d like it to be as quick as something like a for loop as thats nearly instant
how can I achieve this?

RunService.Heartbeat might be a little faster, but not very much.

1 Like

I took out the task.wait() and its not crashing
i thought wait was needed for loops like that

you can use stepped! When it reaches the value, disconnect the stepped connection!

local RunService = game:GetService(“RunService”)
local StepConnect

StepConnect = RunService.Stepped:Connect(function()
–other code here
if CheckingPosition == MaxPosition then
StepConnect:Disconnect()
end
end)

1 Like

At that point just interpolate the value to the desired position in which case just use TweenService and or your own interpolation system using RunService

Note: With RunService if on the client, make you use deltaTime to keep the effect at the same speed. (FPS heavily affects its speed)

Sorry i forgot to Mark last post as solution because its running instantly now without the task.wait
thank you, i would use Stepped but this code is ran on client and server

You’ve already solved it, but I will make a couple notes just to ensure that the issues are fully understood (either by you or if someone ends up on this response later looking for a solution to their own problem)

When your code is running, roblox can’t move on to rendering and stuff because they don’t know what you might change. This means that if your code takes too long to run it can cause frame stutters or flat out crash the game. Putting task.wait() into your code basically just tells it to stop and run again from there next frame which gives roblox time to render and do the other stuff it needs to do to keep the game running before it tells your loop to continue.

I will also note that you don’t have to always task.wait() on every iteration. If you do, you are forcing it to wait a full frame every iteration even if you could run it a couple hundred iterations without a problem. To fix that I usually will add a conditional wait like this for heavy loops.

local MAX_EXECUTION_TIME = 1/120 --Can only run for 1/120th of a second before it passes on control back to roblox temporarily
local lastWait = tick()

for i=1, 20000 do
    expensiveFunction()

    if tick() - lastWait > MAX_EXECUTION_TIME then
        task.wait()
        lastWait = tick() --Have to update after wait so that the wait time isn't included
    end
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.