Gun Firerate Slows Down With FPS Drops, I need solutions

Doesn’t seem to crash for me, unless I have some kind of wait function in place already.

All I know is that the Minigun shoots as fast as it’s supposed to when the game’s not running badly. Which is about 10-20 bullets a second last I checked.

Lag is a problem that you need to attack by determining what you can make more efficient. Do this by following this procedure:
Are my users complaining about lag?
Is this affecting gameplay?
If you answer yes to any of those questions there is a problem, and your next step is determining what is causing all this lag you are facing.

You said that your pain screen lags… What is the current code for that ? There’s no reason for that to lag.

How many FPS are you users getting?

here’s a very simple “gun” script which will shoot 60 bullets per second completely independent of the user’s current frame rate:

local RenderStepped = game:GetService("RunService").RenderStepped
local Mouse = game.Players.LocalPlayer:GetMouse()
local Debris = game:GetService("Debris")
local shooting = false

local function shoot()
	
	local function spawnBullet()
		local bullet = Instance.new("Part")
		local bodyVelocity = Instance.new("BodyVelocity")
		bullet.Size = Vector3.new(1, 1, 1)
		bullet.CanCollide = false
		bullet.CFrame = game.Players.LocalPlayer.Character.Head.CFrame * CFrame.new(0, 0, -2)
		bullet.Parent = game.Workspace
		bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		bodyVelocity.Velocity = bullet.CFrame.LookVector.Unit * 500
		bodyVelocity.Parent = bullet
		Debris:AddItem(bullet, 10)
	end
	
	local deltaT = 1/60 -- fire rate
	local accumulator = 0
	local frameTime = 0
	
	shooting = true
	spawnBullet()
	while shooting do
		accumulator = accumulator + frameTime
		while accumulator >= deltaT do
			spawnBullet()
			accumulator = accumulator - deltaT
		end
		frameTime = RenderStepped:Wait()
	end
end

Mouse.Button1Down:connect(function()
	shoot()
end)

Mouse.Button1Up:connect(function()
	shooting = false
end)
8 Likes

Are the bullets being deleted after a while?

1 Like

Yes.
I’ve noticed recently that I get increasingly more lag every 10 seconds.

I’m wanting to say it has to do with the projectile weapons, but they’re removed after 30 seconds.

Here’s the game if anyone’s interested in seeing what the problem is. I don’t know if you guys will experience it to the extent I am.

Last question, are the bullets being ceased to exist? (Meaning they dissapear from the map entirerly)
If so it’s probally the script overcharge that the game has, probally the other scripts are lagging the other one because that one is charging while the other ones are charging.
If not, it may be the splashing amount of accumulated bullets from the world, if it’s a pump shutgon, it makes 6-8 bullets per 2 seconds (If spamming click) if you do the maths, that will be 4 bullets maximum stored.

1 Like

Also, when you first join a map, the Console is spammed with remote event errors, saying that a RemoteEvent is exhausted.

I might have a separate event to handle sending said projectiles to the clients, instead of relying on a single one to receive the single and send it to the rest of the players.

Fixed the errors mentioned.

My game looks like this occasionally.
Is there anyway to reduce the lag caused by these without nerfing the Plasma Rifle?

1 Like

Actually, I think the problem is my method of handling my custom explosions.

Can anyone send me in the direction of figuring out raycast explosions so I don’t have to rely on GetDescendants?

I’m planning on using Region3s, but you apparently can do Raycast explosions too.

Seems like an entirely different topic. Would you create a new topic, to make this more searchable for future people looking for the same answers?

Apologies for the long “accept.”

Recently ran into this issue again after thinking I had fixed it, came across this thread, and implemented your system above into my game and it works flawlessly.

Thank you!! :slight_smile:

Massive necropost but this code is very easily broken by clicking your mouse faster than the firerate

This is how I went about fixing it:

local Heartbeat = game:GetService("RunService").Heartbeat
local UIS = game:GetService("UserInputService")
local Debris = game:GetService("Debris")
local shooting = false
local wishShooting = false

local function shoot()

	local function spawnBullet()
		local bullet = Instance.new("Part")
		local bodyVelocity = Instance.new("BodyVelocity")
		bullet.Size = Vector3.new(1, 1, 1)
		bullet.CanCollide = false
		bullet.CFrame = game.Players.LocalPlayer.Character.Head.CFrame * CFrame.new(0, 0, -2)
		bullet.Parent = game.Workspace
		bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		bodyVelocity.Velocity = bullet.CFrame.LookVector.Unit * 500
		bodyVelocity.Parent = bullet
		Debris:AddItem(bullet, 10)
	end

	local deltaT = 60 / 600 -- fire rate
	local accumulator = 0
	local frameTime = 0

	shooting = true
	spawnBullet()
	while shooting do
		accumulator = accumulator + frameTime
		while accumulator >= deltaT do
			if wishShooting == false then
				shooting = false
			else
				spawnBullet()
			end
			accumulator = accumulator - deltaT
		end
		frameTime = Heartbeat:Wait()
	end
end

UIS.InputBegan:Connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
		wishShooting = true
		if shooting == false then
			shoot()
		end
	end
end)

UIS.InputEnded:Connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
		wishShooting = false
	end
end)