I can't figure out why my pistol fires one last time after when the bolt retracts back (no bullets left). I've tried to fix it but I don't know why it keeps firing 1 extra time, help? I've also removed the local and main script code due to the character limit. I can post it in the comments, I believe it's in the effects script anyways.

Effects script

local gunshot = Instance.new("Sound", script.Parent)
			gunshot.Parent = script.Parent.Parts.Flare
gunshot.SoundId = "rbxassetid://402318477"
gunshot:Play()
game.Debris:AddItem(gunshot, 0.01)

local gunshot = Instance.new("Sound")
			gunshot.Parent = script.Parent.Parts.Flare
			gunshot.SoundId = "rbxassetid://356911785"
gunshot:Play()
game.Debris:AddItem(gunshot,0.01)

local Bolt = script.Parent.Parts:FindFirstChild("Bolt"):GetChildren()
local BoltMid = script.Parent.Parts:FindFirstChild("BoltMid"):GetChildren()
local BoltEnd = script.Parent.Parts:FindFirstChild("BoltEnd"):GetChildren()
local Reload = script.Parent.Reload
local Reloading = false
local ammo = 17 --17 here
local Val = script.Parent:FindFirstChild("AbleToShoot")
Reload.OnServerEvent:Connect(function()
	if Reloading == false then
    script.Parent.Local.Reloading.Value = true
		ammo = 0
		Val.Value = false
		local gunshot = Instance.new("Sound", script.Parent)
			gunshot.Parent = script.Parent.Parts.Flare
gunshot.SoundId = "rbxassetid://402318477"
gunshot:Play()
game.Debris:AddItem(gunshot, gunshot.TimeLength)
				for i, v in pairs(Bolt) do
	v.Transparency = 1
	end
	for i, v in pairs(BoltMid) do
		v.Transparency = 0
	end
		wait(0.05)
		for i, v in pairs(BoltMid) do
			v.Transparency = 1
		end	
		for i, v in pairs(BoltEnd) do
			v.Transparency = 0
			end
		local animation = script.Parent.Parent:FindFirstChild("Humanoid"):LoadAnimation(script.Parent.Animation)
		animation:Play()
			local mag = script.Parent.Parts:FindFirstChild("Magazine")
			mag.Transparency = 1
			local magC = mag:Clone()
			magC.Parent = game.Workspace
			magC.Transparency = 0
			magC.CanCollide = true
			game.Debris:AddItem(magC, 7.5)
			wait(0.95)
			mag.Transparency = 0
			wait(0.4)
					for i, v in pairs(BoltEnd) do
			v.Transparency = 1
		end
		for i, v in pairs(BoltMid) do
			v.Transparency = 0
		end
		wait(0.025)
		for i, v in pairs(BoltMid) do
			v.Transparency = 1
		end
		for i,v in pairs(Bolt) do
			v.Transparency = 0
			end
			wait(0.525)
			Val.Value = true
			ammo = 17
			end
	end)

script.Parent.BulletEvent.OnServerEvent:Connect(function(Player, FromP, ToP, BulletPos)
	if ammo > 0 then
				ammo = ammo-1
		local gunshot = Instance.new("Sound")
			gunshot.Parent = script.Parent.Parts.Flare
			gunshot.SoundId = "rbxassetid://356911785"
gunshot:Play()
script.Parent.Bullet.Casing.Transparency = 0
local eject = script.Parent.Bullet.AnimationController:LoadAnimation(script.Parent.Bullet.Animation)
eject:Play()
wait(0.12)
script.Parent.Bullet.Casing.Transparency = 1
game.Debris:AddItem(gunshot, gunshot.TimeLength)
script.Parent.Parts:FindFirstChild("Flare").MuzzleFlash.Enabled = true
script.Parent.Parts:FindFirstChild("Flare").MuzzleFlash.Rotation = NumberRange.new(math.random(0,1500))
local smoke = script.Parent.Parts:FindFirstChild("Flare").Smoke:Clone()
smoke.Parent = script.Parent.Parts:FindFirstChild("Flare")
smoke.Enabled = true
game.Debris:AddItem(smoke,2)
local smoke2 = script.Parent.Parts:FindFirstChild("Flare").Smoke2:Clone()
smoke2.Parent = script.Parent.Parts:FindFirstChild("Flare")
smoke2.Enabled = true
local steam = script.Parent.Parts:FindFirstChild("Flare").Steam:Clone()
steam.Parent = script.Parent.Parts:FindFirstChild("Flare")
steam.Enabled = true
game.Debris:AddItem(smoke2,2)
		for i, v in pairs(Bolt) do
	v.Transparency = 1
	end
	for i, v in pairs(BoltMid) do
		v.Transparency = 0
	end
		wait(0.0125)
		for i, v in pairs(BoltMid) do
			v.Transparency = 1
		end	
		for i, v in pairs(BoltEnd) do
			v.Transparency = 0
		end
		if ammo > 0 then
		   wait(0.0125)
		for i, v in pairs(BoltEnd) do
			v.Transparency = 1
		end
		for i, v in pairs(BoltMid) do
			v.Transparency = 0
		end
		wait(0.0125)
		for i, v in pairs(BoltMid) do
			v.Transparency = 1
		end
		for i,v in pairs(Bolt) do
			v.Transparency = 0
		end
		end
		wait(0.1625)
	elseif ammo == 0 and Val.Value == true then
Val.Value = false
		Reloading = true
		ammo = 0
script.Parent.Local.Reloading.Value = true
		Val.Value = false
		local gunshot = Instance.new("Sound", script.Parent)
			gunshot.Parent = script.Parent.Parts.Flare
gunshot.SoundId = "rbxassetid://402318477"
gunshot:Play()
game.Debris:AddItem(gunshot, gunshot.TimeLength)
		local animation = script.Parent.Parent:FindFirstChild("Humanoid"):LoadAnimation(script.Parent.Animation)
		animation:Play()
			local mag = script.Parent.Parts:FindFirstChild("Magazine")
			mag.Transparency = 1
			local magC = mag:Clone()
			magC.Parent = game.Workspace
			magC.Transparency = 0
			magC.CanCollide = true
			game.Debris:AddItem(magC, 25)
			wait(0.95)
			mag.Transparency = 0
			wait(0.4)
					for i, v in pairs(BoltEnd) do
			v.Transparency = 1
		end
		for i, v in pairs(BoltMid) do
			v.Transparency = 0
		end
		wait(0.025)
		for i, v in pairs(BoltMid) do
			v.Transparency = 1
		end
		for i,v in pairs(Bolt) do
			v.Transparency = 0
			end
			wait(0.575)
			Val.Value = true
			ammo = 17
			Reloading = false
	end
end)

There are some comments I used to organize things and for testing. Sorry if this is long but I really can't figure out why it keeps firing 1 extra time.

1 comment

0

Nevermind, figured it out myself