Everything becomes slowmo at FPS greater than 60

Result in studio (60 FPS)
Result in game (400+ FPS)

I am not able to explain verbally so see the videos above.

Here is the code that moves the things:

local function Step(dt)
	for i,v in pairs(Gameplay:GetChildren()) do
		if v:FindFirstChild("Speed") then
			v.Position += UDim2.new(0,0,0,v.Speed.Value * 60*dt)
			if v.AbsolutePosition.Y > Gameplay.AbsoluteSize.Y+10 then
				v:Destroy()
			end
		end
	end
end
8 Likes

it looks like v.Position += UDim2.new(0,0,0,v.Speed.Value * 60*dt) is the problem
seems like it was written assuming that all clients are running @ 60fps
it should be changed to:
v.Position += UDim2.fromOffset(0, v.Speed.Value * dt)

at 60fps the value of dt is 1/60, which is around 0.016… you’re cancelling it out by doing 60 * dt
at 400fps the value of dt is 1/400, which is around 0.0025… which is being multiplied by 60 and causes the sprites/environment to move at 15% of the intended speed


also as a side note since this function is being called every frame, i suggest using some sort of cache system to replace the GetChildren() & FindFirstChild() functions because they’re slow

6 Likes

I will do that but wont it be even slower? and if dt is 1/400 will it not mean that runservice.RenderStepped. The thing I am using to run the Step function. Shouldn’t it run 400 times a second?

I will. The game is not even near completion and its just some test bugs I found.

3 Likes


image

No, it won’t be slower
i drew a pretty image demonstrating that an object travelling at 5 units per second will travel 5 units in 1 second regardless of framerate:

4 Likes

The 400fps one compensates its speed by doing it ~666% faster though (400/60), which gives us the same speed (0.15 * 6.667 = ~0.999 = 1)

The * 60 part doesn’t matter since it’s multiplied to both calculations, I think OP * 60 so the speed value is more manageable.

it may help to print out the value of dt and the time elapsed between each step (is the calculation too heavy that it has no choice but to yield?). Setting your FPS cap to a value below 400 like 240 or 120 might help to see if it still work as intended.

3 Likes

I’m not very smart

It’s going in slow-motion because OP is adding the UDim position by pixels, and not by scale.

Pixels don’t support decimals which is why the movement is inconsistent

OP should be using scale which supports decimals

local APS = v.Speed.Value / Gameplay.AbsoluteSize.Y
v.Position += UDim2.fromScale(0, APS * 60 * dt)
2 Likes

You should change v.Speed.Value to the amount you want to move in 1 second and remove the 60*.

1 Like

Damn I did not think about that and hey I tested it and you deserve this now.

1 Like

Thats quite a stupid solution honestly.

1 Like

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