How to make my weapon fire multiple times per shot to keep up with the firerate?

I want my weapon to fire multiple times if needed incase it cannot keep up with the firerate of a certain weapon because of fps.

Currently the max firerate is 60 shots per second if the player hasn’t got an fps unlocker and is running roblox at a stable fps. But let’s say I want the firerate to be 120 shots per second which means it should fire twice per second or if the player has 30 fps but the gun fires 60 shots per second it also has to fire twice but I have no idea how to figure out when to fire multiple times.

I have tried looking for similar topics and asking for help on discord servers but nobody was able to help me, so I would very thankful if someone helped me figure it out!

(also the firerate is formatted in shots per minute which is why I am using 60/firerate)

local FireConnection
local Start = tick()
local LastShotTick = tick() - 60/Settings.Firerate
local Shots = 0
FireConnection = RunService.Stepped:Connect(function(dt)
	if tick() - LastShotTick > 60/Settings.Firerate and tick() - Start < 60 then
		--Fire weapon
		LastShotTick += 60/Settings.Firerate
	end
	if not Active then
		FireConnection:Disconnect()
	end
end)
3 Likes

You could move the actual “shooting” bit to the server. Then you tell when you start firing and when you stop firing. This also helps preventing exploiters from firing the gun more than it’s supposed to/having infinite ammo.

You can keep your current code for effects like bullet tracers which don’t matter as much.

1 Like

Roblox servers will also always run at 60 FPS (as long as nothing too intensive is happening) so you don’t need to worry about FPS unlockers either.

1 Like

Yeah I’m planning on doing that and I got an idea of how I can do it with lag compensation and stuff but the server still runs at 60 fps max which would still result in the same issue

1 Like

…Isn’t that what you want though? Use the server’s consistent framerate instead of the player’s?

Yeah the server does have more consistent framerate which allows the weapon to fire 60 time per second, but if I want the weapon to have more than 60 shots per second it would result in shots that weren’t fired cause the server has 60 fps

You could double the damage to compensate for that if you want the gun to fire that fast. Also are you making a laser weapon? Why would you need to fire that fast in the first place?

I want to add it just in case, cause the server can also lag which would result in a few shots being not fired cause it might not update as fast

At that point you might want to think about optimising your scripts if the server is experiencing performance issues…

If the server lags, all your players also experience a ping spike, which means that nobody gains/loses the advantage.

just use floor division to figure out if multiple shots can be fired.

My scripts are optimized, I want to add this compensation for weapons such as a minigun which have an insane rate of fire of up to 6000rpm which results in roughly 100 shots per second

I tried using divisions, but it had some results where it would either nerf the firerate or buff it somewhat randomly

actually nevermind i think i made a mistake

I add the firerate instead of setting it to the tick cause it makes the results just that tiny bit more accurate

have you tried

local rate = 60/Settings.Firerate)
local t = tick()- LastShotTick
local shots = math.floor(t/rate)
LastShotTick += shots*rate
3 Likes

shots is the amount of bullets it needs to fire that frame correct?

yes---------------------------- --------yes

I tried it and it works exactly as wanted, firing multiple shots if it lags behind.
Thank you kind stranger!