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.
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
What is the issue? task.wait() waits significantly longer when in actual game and not studio.
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?
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()
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:
while currentElapsed < moveTime do
currentElapsed += RunService.Heartbeat:Wait()
blueBlock.CFrame = greenBlock.CFrame:Lerp(redBlock.CFrame, currentElapsed/moveTime)
end
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)
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)