Task.wait() length different on local server (studio) and Roblox server

Hello,
Let’s say I’m lerping a part on a server, sometimes hundreds of parts, based on speed variable, every position change I use task.wait(), and no matter the amount of parts the movement speed is slower in Roblox player than in studio. Even with a singular part the difference is very easily noticable, and breaks the game.

  1. What do you want to achieve? I want to have task.wait() wait the same time no matter the server performance or whatever it’s called

  2. What is the issue? task.wait() waits significantly longer when in actual game and not studio.

  3. What solutions have you tried so far? Haven’t tried much, but I did some research, could using a custom wait module or checking server time help?

Also, is there any alternative to task.wait() that even if set for like 60 seconds can be stopped any moment?

1 Like

If you are using task.wait in order to change the position of the parts (lerp), then I would advise you another way out:

local RunService = game:GetService("RunService")
local part1 = ws:WaitForChild("Part1")
local part2 = ws:WaitForChild("Part2")
local part3 = ws:WaitForChild("Part3")

local currentTime = 0
local moveTime = 5

local function lerp(startPos, endPos, t)
	return startPos + (endPos- startPos) * t
end
while currentTime < moveTime do
	currentTime += RunService.Heartbeat:Wait()
	part3.Position = lerp(part1.Position, part2.Position, math.min(currentTime/moveTime, 1))
end

Also, if you wanna to stop movement at any time, you should add another variable:

local RunService = game:GetService("RunService")
local ws = game:GetService("Workspace")
local part1 = ws:WaitForChild("Part1")
local part2 = ws:WaitForChild("Part2")
local part3 = ws:WaitForChild("Part3")

local currentTime = 0
local moveTime = 30
local shouldMove = true

local function lerp(startPos, endPos, t)
	return startPos + (endPos - startPos) * t
end

local function stopMovement()
	shouldMove = false
end

spawn(function()
	while currentTime < moveTime and shouldMove do
		currentTime += RunService.Heartbeat:Wait()
		part3.Position = lerp(part1.Position, part2.Position, math.min(currentTime / moveTime, 1))
	end
end)

task.wait(2)
stopMovement()
1 Like

Damn I really couldnt think of it, It feels kinda genius lol, thanks a lot man, nah actually thanks a bit cuz that’s ai for sure, still helpfull

2 Likes

Firstly, you’re welcome, dude.

Secondly, I used ai only in the second part (where I had to stop the while loop). I found out the first part of the script myself when I watched various posts about bezier curve. It just said about lerp:

How to make lerp Bezier Curves with RunService! [Chapter 1] - Resources / Community Tutorials - Developer Forum | Roblox

while currentElapsed < moveTime do
	currentElapsed += RunService.Heartbeat:Wait()
	blueBlock.CFrame = greenBlock.CFrame:Lerp(redBlock.CFrame, currentElapsed/moveTime)
end
2 Likes

You can stop task.wait if it is in a thread.

local thread = task.spawn(function()
    -- I run asynchronously, meaning that I run in parallel to other code in this script
    task.wait(60)
    print("Hooray, I'm alive!")
end)

task.cancel(thread)
1 Like

I believe the reason task.wait() is faster in studio than on the client is because, in studio when doing a normal playtest, the server runs at the same frame rate as the client, and now that the client can run faster than 60 fps (given you changed that in the settings), the server will do so as well. This is a bug in and of itself, as Roblox servers will never run faster than 60 fps
(task.wait(), when set to a time small enough, will wait for a single frame)

Either way, @Super_God567’s solution is the proper way to move parts, as it is independent from fps

Next time, please share your code when you post in #help-and-feedback, since I had to do some assumptions with my reply (such as assuming your code uses task.wait(0) or task.wait(), as task.wait(1) or other value higher than like a couple ms will still wait for about the same amount of time regardless of fps)

2 Likes

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