My gun is firing multiple bullets at once after changing coding

So I previously had a bug where my animtrack wasnt stopping the animations when I unequipped my tool so I changed my code so there were seperate animTracks for each animation but now my gun fires multiple bullets at once

Here is a video:
https://imgur.com/a/teHcDLf

Here is my firing code:

Revolver.Activated:Connect(function()
	local startPos = Revolver.Handle.CFrame.Position
	local endPos = Mouse.Hit.Position
	if canShoot and ammo > 0 then
		IdleTrack:Stop()
		EquipTrack:Stop()
		ReloadTrack:Stop()
		
		canShoot = false
		ShootTrack:Play()
		
		ShootTrack:GetMarkerReachedSignal("Fire"):Connect(function()
			ShootSound:Play()
			ammo -= 1
			print(ammo)
			canReload = true
			ShootEvent:FireServer(startPos, endPos, MaxDistance)
		end)
		
		ShootTrack.Stopped:Wait()
		canShoot = true
		IdleTrack:Play()
	end
end)

If anyone knows why please let me know! (and optionally if you know a fix on why my animations didn’t stop playing when I unequipped my gun, that would be helpful too)

Also here is my serverside script that handles spawning the bullet:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ShootEvent = ReplicatedStorage.Events.ShootEvent

ShootEvent.OnServerEvent:Connect(function(player, startPos, endPos, maxDistance)
	local RayCast = Ray.new(startPos,(endPos - startPos).Unit * maxDistance)

	local workspaceRay = workspace:Raycast(startPos, endPos)

	local laser = Instance.new("Part",workspace)

	laser.BrickColor = BrickColor.new("New Yeller")
	laser.Material = Enum.Material.Neon
	laser.Transparency = 0.5
	laser.CanCollide = false
	laser.Anchored = true
	local distancePart = (startPos - endPos).magnitude
	laser.Size = Vector3.new(0.3,0.3,distancePart)
	laser.CFrame = CFrame.new(startPos,endPos) * CFrame.new(0,0,-distancePart / 2)

	task.wait(0.1)
	laser:Destroy()
end)

The reason why your gun is firing multiple bullets is because of how you are handling the ShootTrack:GetMarkerReachedSignal("Fire"):Connect(function() inside of your Revolved.Activated function.

Since you have ShootTrack:GetMarkerReachedSignal("Fire"):Connect(function() inside of this function, every time you click to shoot, it is adding a new connection to the "Fire" marker. ROBLOX isn’t automatically cleaning up your old connections, so each time the animation hits that "Fire" marker, all previous connections are also firing - which is causing multiple bullets to be shot at once. This aligns with what you have shown in the video too.

You should move the .Connect call outside the Activated function so that it runs only once when the script starts. That way, only a single connection exists regardless of how many times the gun is fired.

If you need to access dynamic values like startPos, endPos, or MaxDistance, you can store those in variables just before playing the animation, or use function arguments to pass them in.

1 Like

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