Tool gun fire rate just breaking

im making a gun system and trying to make the fire rate part of it

but… the time is uneven, somehow.

local weapon = script.Parent

local rp = game:GetService("ReplicatedStorage")

local equipped = false

local equipAnim = script:FindFirstChild("WeaponEquip")
local idleAnim = script:FindFirstChild("WeaponIdle")
local unequipAnim = script:FindFirstChild("WeaponUnequip")
local shootAnim = script:FindFirstChild("WeaponShoot")

local weaponClone = rp.Guns:WaitForChild("Pistol") -- change this to the weapon model

local weaponParticles = weapon:WaitForChild("GunParticle")

local ShotSound = weapon:WaitForChild("ShotSound")

local canShoot = false

local gunFireRate = 1

weapon.Activated:Connect(function()
	local hum = script.Parent.Parent.Humanoid

	if canShoot == false then
		canShoot = true
		
		-- particles
		local muzzle = weaponParticles:WaitForChild("MuzzleFlash")
		local sparkle = weaponParticles:WaitForChild("Sparkle")
		local smoke = weaponParticles:WaitForChild("Smoke")

		-- anim tracks
		local shootAnimTrack = hum:LoadAnimation(shootAnim)

		shootAnimTrack:Play()

		ShotSound:Play()

		muzzle.Enabled = true
		sparkle.Enabled = true
		smoke.Enabled = true

		wait(0.05) -- change as needed

		muzzle.Enabled = false
		sparkle.Enabled = false
		smoke.Enabled = false
		
	end
	
	
	
	task.wait(gunFireRate)
	
	print(gunFireRate)

	canShoot = false
	

end)

this could be cause of the tool activated but idk

video:

Try printing the value of canShoot at the beginning of weapon.Activated to debug. That’s my best idea as I don’t necessarily see anything wrong with the code. However, it’s a hard read personally.

I don’t know of that you have an FPSUnlocker enabled, but I believe task.wait() is frame rate dependent, so changing it to wait() could help. Might be wrong.

sorry for the hard to read code
it counts weird for some reason

(wait still does the same)

Is this script server sided? If it is that’s your problem.

yeah it is

Summary

charactercharacters

That’s where your inconsistencies should be coming from. Most of the time, anything that has to do with the player should be handled on the client and not the server.

so how could i update my scripts?

i fixed it i shouldve used a debounce :man_facepalming:

this helped me btw: How do i use delay in tools?

final code:
if canShoot then
canShoot = false

	-- particles
	local muzzle = weaponParticles:WaitForChild("MuzzleFlash")
	local sparkle = weaponParticles:WaitForChild("Sparkle")
	local smoke = weaponParticles:WaitForChild("Smoke")

	-- anim tracks
	local shootAnimTrack = hum:LoadAnimation(shootAnim)

	shootAnimTrack:Play()

	ShotSound:Play()

	muzzle.Enabled = true
	sparkle.Enabled = true
	smoke.Enabled = true

	wait(0.05) -- change as needed

	muzzle.Enabled = false
	sparkle.Enabled = false
	smoke.Enabled = false
	
	wait(1)

	canShoot = true

	
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.