How To Use Delta Time?

  1. I’ve made a good gun system, but it has one issue, on laggy computers or consoles, guns shoot slower since it is running off of local time.

2.currently I am useing game:GetService(“RunService”).Heartbeat:Wait() which works great, the problem is when a player has low fps it runs slower, and their gun will shoot slower.

  1. I have tried using wait() but the minimum wait time for that is 0.03 I can’t get accurate enough timing
								if Rpm < 1200 then
									if 60/Rpm - addedframetime > 0.016 and 60/Rpm - addedframetime < 0.03 then			
										for i = 0,(60/Rpm) - addedframetime,0.016 do
											game:GetService("RunService").Heartbeat:Wait()		
										end	
									else			
										for i = 0,(60/Rpm) - addedframetime,0.03 do
											wait()	
										end										
									end
								end

Thanks For Your Help , Icecatz

This seems like a bad system in general if your gun shoots slower just because the device runs slower. The gun should shoot at the framerate of the server. You should never have something as crucial as a gun shooting tied to the client’s time.

What you should be doing is sending the input to the server, then have the server do the calculations for the time based on Run Service that way if the server is slowing down, it will account for it.

If all else fails, try using os.time() as a yield which will give you the actual time according to the local user. It will be accurate at the very least.

3 Likes

If i make the waiting function on server i would still have to send over the times via remote function, it will take additional time for the remote to be sent across server client.

What specific part of the gun shooting is using the local wait time? Is it bullet travel time? Automatic fire? What exactly can’t just be transferred over to the server?

I’m not saying to move only the wait time to the server, I’m saying it may help to move the entire thing over to the server.

Moving an entire gun script to the server would be stupid,since server cannot get keyboard imput or mouse imput it would feel extremely laggy, the part that it is waiting for here is automatic fore , the time between shots

Moving the entire gun to the server is a way to secure your gun completely. But I do see your point on the lag.

To answer your question you may just want to use time() or tick(). Store the current tick when you fire your bullet, then the next time you check in whatever loop you’re running you would do tick() - lastTick and compare the difference. If it is greater than your threshold then fire again and store the tick. Repeat.

This does still have the problem of lower framerates having slower firespeed but the difference should be negligible and is more reliable than just a simple wait(). Of course, if the player is running at such low fps that it can’t properly run that check every second, then the player is probably having other problems besides shooting.