While true loop running faster with an fps unlocker

So using an fps unlocker my projectiles fly faster, I tried to fix this. As you can see in the gayzo it’s really choppy it and it doesn’t even work when I use an fps unlocker or it does but it makes my projectile over lap. I’m trying to make the while true loop run at 60 fps.
https://gyazo.com/a28ee795963738d6657cc42cf787f36a
https://gyazo.com/950df3a4e0cb2c2f9ecd4cbe29ad2452

Anyone got any solutions

while true do
			local DT = game:GetService("RunService").Heartbeat:Wait()
			local movement = 60/DT

			--positions
			number = number + 1
			local position = (4*100*DT) * number  --4 is the length of each part in the projectile, 100 is the speed and dt is the delta time
end
3 Likes

That’s because hearbeat runs every frame. The higher your frames per second the more it runs.

Nevermind I see you using the delta time property.

3 Likes

Heartbeat is tied to the users frame-rate. This could give players an in-game advantage. I would suggest using something else.

Heartbeat is fine, and you’re close to using it correctly but not quite.

You can simplify your code a bit:

local studsPerSecond = 100

while true do
	local dt = game:GetService("RunService").Heartbeat:Wait()

	local position = position + studsPerSecond*dt
end
6 Likes

It seems weird for that to happen. The value returned by the Heartbeat event should be the time in seconds since the previous frame, so ‘DT’ should be smaller if the frame rate is higher.

Have you tried running to benchmark to see if the FPS unlocker actually changes that?

local rs = game:GetService("RunService")
local frameCount = 0
local timeCount = 0
repeat
	timeCount = timeCount + rs.Heartbeat:Wait()
	frameCount = frameCount + 1
until (timeCount >= 10)
print(frameCount)

If you ran something like this once without the unlocker then with it turned on, the difference in the frame counters should be about proportional to the change in FPS

1 Like

By setting movement to 60/DT, movement will increase as DT decreases. When does DT decrease? When the framerate increases.

1 Like

That’s some strange behavior and is definitely going to change a few things about how my stuff is coded. Thank you for posting this.

As a fix, you could try measuring the timestep yourself:

while true do
	local tStart = os.clock()
	game:GetService("RunService").Heartbeat:Wait()
	local DT = os.clock() - tStart

	--Remainder of your code here
end

os.clock() directly talks to your CPU clock and thus should give you a more accurate measurement.

1 Like

You don’t need to divide 60 by DT anyway. Multiplying the pre-defined velocity by DT will result in the distance the object should move in that one frame.

1 Like

Still getting the same choppy behavior.

This is the result not sure whats wrong. https://gyazo.com/18511c77cf1736d1115d358d648a3339

How am I close? am I not using it correctly

Using while wait() is too slow and not as smooth

Interesting. I do have a question before I make any further assumptions:

You have the line local movement = 60/DT. What is this used for? Keep in mind, DT is a time in seconds, so movement will actually be a higher value for higher framerates. If you’re basing the projectile’s movement on this, there’s no surprise it’s moving faster at higher framerates.

Your position calculation is more correct for what you’re trying to achieve.

Keep in mind as well that framerates can be unstable, especially with an FPS unlocker. This can cause choppy behavior.

1 Like

I want the loop to run at 60 fps.

Trying to force the loop to run at a specific rate is probably what caused your issue in the first place. You should write your scripts to be able to run at any physics rate. That’s part of why functions like wait() and RenderStepped:Wait() return the amount of time that was yielded - so you can use that value to accurately move things at the speed you want.

Yes, that’s what I’m trying to do as you can see from my attempts it hasn’t worked out.

You were at least attempting to scale by dt, but doing it wrong. I gave you the correct way to do it in my previous reply:

1 Like

that just makes it slow in studio and instant in-game.

Edit: wait nvm lol thanks

Nvm it’s doesn’t do anything, I’m trying to make it run at 60 fps no matter what. I might just make a script to kick you if you’re using an fps unlocker because nothings working. https://gyazo.com/7b81ee8d6da5d2013ee4a72601912f68

I don’t understand what your video shows, it looks like it’s working fine to me :slight_smile:

The code I posted is correct. position will increase at the same rate on all computers regardless of your FPS.

1 Like