How do i add a reloading system to this script

Hello, so i got this script for my gun (its a local script)

local gun = script.Parent

local gun_shot = game.ReplicatedStorage["Gun shot"]


local ReplicatedStorage = game:GetService('ReplicatedStorage')

local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent2')


local shooting = false

local equipped = false


local player = game.Players.LocalPlayer

local mouse = player:GetMouse()
 local reloading = false

local mouseConnection


gun.Equipped:Connect(function()



	equipped = true



	mouseConnection = mouse.Button1Down:Connect(function()
		if not shooting and not reloading and gun.Ammo.Value > 0 then
gun.Flash.BillboardGui.Enabled = true
		shooting = true
		gun.Flash.BillboardGui.Enabled = true
			gun.Flash.BillboardGui.Enabled = true
			remoteEvent:FireServer(gun.Handle.Position, gun.Handle.Orientation, mouse.Hit.p)
			gun.Flash.BillboardGui.Enabled = true
			gun_shot:Play()
			gun.Smoke.ParticleEmitter:Emit()
			gun.Flash.BillboardGui.Enabled = true
			gun.Ammo.Value -= 1
        mouse.Button1Up:Connect(function()
				gun.Flash.BillboardGui.Enabled = false
				shooting = false
			end)
		end
		wait(0.1)
	end)


gun.Unequipped:Connect(function()

	equipped = false

	mouseConnection:Disconnect()
end)
end)

i tried this:

elseif gun.Ammo.Value == 0 then
		game.ReplicatedStorage.clip_empty:Play()
        mouse.Button1Up:Connect(function()
				gun.Flash.BillboardGui.Enabled = false
				shooting = false
			end)
		end
		wait(0.1)
	end)


gun.Unequipped:Connect(function()

	equipped = false

	mouseConnection:Disconnect()
end)
end)

sadly it doesn’t work, i just shoot the gun and it doesnt work after 1 shot. could anyone help?

1 Like

Do you get an error? If so, please tell me.

No i don’t get an error, let me record and show you what i mean.

You probablly dont know whats happening because of the LAG, but let me explain. i basically shoot once and when i shoot again, it just doesnt make the sound meaning it doesn’t fire the bullet which is suppost to deal damage. (also the reload thingy doesnt work)

Can I ask where did you put the else if statement for reloading?

before the

mouse.Button1Up:Connect(function()

part

local gun = script.Parent

local gun_shot = game.ReplicatedStorage["Gun shot"]


local ReplicatedStorage = game:GetService('ReplicatedStorage')

local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent2')


local shooting = false

local equipped = false


local player = game.Players.LocalPlayer

local mouse = player:GetMouse()
 local reloading = false

local mouseConnection


gun.Equipped:Connect(function()



	equipped = true



	mouseConnection = mouse.Button1Down:Connect(function()
		if not shooting and not reloading and gun.Ammo.Value > 0 then
gun.Flash.BillboardGui.Enabled = true
		shooting = true
		gun.Flash.BillboardGui.Enabled = true
			gun.Flash.BillboardGui.Enabled = true
			remoteEvent:FireServer(gun.Handle.Position, gun.Handle.Orientation, mouse.Hit.p)
			gun.Flash.BillboardGui.Enabled = true
			gun_shot:Play()
			gun.Smoke.ParticleEmitter:Emit()
			gun.Flash.BillboardGui.Enabled = true
			gun.Ammo.Value -= 1
elseif gun.Ammo.Value == 0 then
		game.ReplicatedStorage.clip_empty:Play()
        mouse.Button1Up:Connect(function()
				gun.Flash.BillboardGui.Enabled = false
				shooting = false
			end)
		end
		wait(0.1)
	end)



        mouse.Button1Up:Connect(function()
				gun.Flash.BillboardGui.Enabled = false
				shooting = false
			end)
		end
		wait(0.1)
	end)


gun.Unequipped:Connect(function()

	equipped = false

	mouseConnection:Disconnect()
end)
end)

Here you’re just playing the clip_empty sound, do you want to reload too if the value of gun.Ammo is 0? You could implement a reloading system when pressing R.

i know how to script that, im just saying that when i press r it doesn’t play the sound meaning its broken and also, when i shoot the gun it only shoots once and doesnt work anymore, (its suppost to be a switch which is basically an automatic pistol)

What’s the initial value of gun.Ammo?

Just a heads up, it’s best practice to handle reloading on the server side. Also can you please add a print command to see if the if statement is even going through.

Yeah, as well as handling the damage in the server but I believe he already does that.

okay so it goes through but i still don’t know how to fix the problem in which the gun shoots once and it doesnt work anymore.

also im pretty stupid because the reloading didn’t work because i didnt even make a function for it.

how do you envision it to work? Are you trying to make a user input activate a reload or a reload after ammo is empty?

i already made the reloading thingy work by myself, thanks for helping (im trying to fix the gun shooting once and not working later)

Can you ensure that mouse.Button1Up is being fired? Put a print after shooting = false.