Let’s say I was making a tower defense game, and wanted to move enemies along a path, there can be several on the screen at the same time, and they are moved every fram with a function called MoveEnemies(deltaTime), with the delta time parameter so they move at a constant speed, my question is if I could do something like this on the server
while true do
local dt = RunService.Heartbeat:Wait() + RunService.Heartbeat:Wait() + RunService.Heartbeat:Wait() + RunService.Heartbeat:Wait()
MoveEnemies(dt)
end
And move them smoothly on the client like so
while true do
local dt = RunService.Heartbeat:Wait()
MoveEnemies(dt)
end
But its also possible do it without looping at all
local startPosition = Vector3.new(0, 0, 0)
local endPosition = Vector3.new(0, 0, 50)
local magnitude = (endPosition - startPosition).Magnitude
local direction = (endPosition - startPosition).Unit
local speed = 4
local startTime = os.clock() -- send this start time to the client so they can calculate the position as well
local function GetPosition()
local deltaTime = os.clock() - startTime
local movedAmount = math.min(speed * deltaTime, magnitude)
return startPosition + direction * movedAmount
end
print(GetPosition())
task.wait(1)
print(GetPosition())
task.wait(5)
print(GetPosition())
task.wait(2)
print(GetPosition())
This took me a bit to understand but it makes sense, I should just use a single task.wait() with a number parameter, because even though the framerate doesn’t match, delta time does.