While true loops running faster in-game

I’m making visual projectiles on the client and it’s using a while true loop, I tested it in-game and it’s wayyyy faster then in studio. I was wondering how to make it the same speed as in studio.

I did a little test I have 1 script printing numbers all the way to 1000 as fast as it can:

local Number = 0 
while true do
	game:GetService("RunService").Heartbeat:Wait()
	Number = Number +1
	
	if Number == 1000 then
		break
	end
	
	print(Number)
end

And I have another counting the seconds

local Number = 0
while wait(1) do
	Number = Number + 1
	print("seconds:"..Number)
end

As this is client sided I’m assuming it does this as fast as your pc can do it. Also this isn’t completely accurate ofc but it shows the difference well enough.

In studio it takes 18 seconds to get to 1000 I tried it twice.
image

In-game I tried it twice 1 time I got 6 seconds and the other i got 3. As you can see thats a pretty substantial difference.
image

2 Likes

Well on studio you host the client and the server on your machine, while ingame roblox’s hosts the server and your computer is connected as a client. This possibly reduces the stress on the server machine making it run faster correct me if i’m wrong.

Yes, but can I not make it a linear speed so it’s the same for everyone because if one players seeing someone get damaged and another is not, its bad player experience especially because my projectiles move semi slow.

Heartbeat:Wait() will return dt (“delta time” aka time passed), so you can adjust based on that. Heartbeat runs once per frame, so tanking the frame rate will inevitably make the event run slower.

7 Likes

How do you propose adjusting it? adding a wait()? I mean if i could manually cap their frames but most people use fps unlockers anyways

Just base whatever per-frame stuff you’re doing by the dt. For example, let’s say your projectile moves at a stud per second. If the dt is a sixtieth of a second, you’d move it a sixtieth of a stud. If it’s twice that, then twice the distance, and so on.

3 Likes

My projectiles are a little different, it has segments each segment is 5 studs long. I tried dividing the segment by the time since last frame but it moves like 0.01 and then jumps 10 studs forward. I’m confused how to make this work.

slow mode of my projectile
https://gyazo.com/563ac17f9b5644caab3207b2f5a1c9b5