How to 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

i forgot to include one more thing that is probablly important, so i already have a script that if i click r then it reloads,

userinputservice.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			local keycode = input.KeyCode
			if keycode == Enum.KeyCode.R and not reloading then
				reloading = true
				wait(cooldown) -- Add a cooldown before allowing shooting again after reloading
				gun.Ammo.Value = 15
				player:FindFirstChild("ParticleEmitter").Parent = gun.Smoke
				player:FindFirstChild("BillboardGui").Parent = gun.Flash
				gun.Magazine.Value = gun.Magazine.Value - gun.Ammo.Value
				reloading = false
			end
		end
	end
end)

Your system has lots of flaws, but this should work.

local UIS = game:GetService("UserInputService")

local function reloadGun()
   reloadSound:Play()
   reloadAnimation:Play()
   gun.Ammo.Value = -- max ammo
end

gun.Activated:Connect(function()
   if gun.Ammo.Value == 0 then
      reloadGun()
   end
end)

UIS.InputBegan:Connect(function(input)
   if input.KeyCode == Enum.KeyCode.R and equipped then
      reloadGun()
   end
end)

I don’t know if it just doesn’t work or if i put it in a wrong line, im trying different lines, so if nothing works i guess the script’s broken.

If you want I can just remake your script.

I’m a bit busy at the moment but I could try.

Okay let me explain a few things, im trying to make a hood game and im trying to add a switch to it (basically an automatic pistol) it was automatic before, but after i tried your script and then deleted it for testing it’s just a semi. Could ya help?

Pretty basic scripts. You should modify them based on your needs.

Local Script:

local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local gunTool = script.Parent
local handle = gunTool:WaitForChild("Handle")

local player = Players.LocalPlayer
local character = player.Character

local reloadButton = Enum.KeyCode.R

local reloadSound = handle.Reload

local isShooting = false
local isReloading = false
local shotCooldown = false
local shotDelay = 0.1

local shootEvent = gunTool.ShootEvent
local reloadEvent = gunTool.ReloadEvent

gunTool.Equipped:Connect(function()
	UIS.InputBegan:Connect(function(input)
		if input.KeyCode == reloadButton and not isShooting then
			if not isReloading then
				reloadEvent:FireServer()
				isReloading = true
				wait(reloadSound.TimeLength)
				isReloading = false
			end
		elseif input.UserInputType == Enum.UserInputType.MouseButton1 and not isReloading then
			if not isShooting then
				isShooting = true
			end
		end
	end)
	
	UIS.InputEnded:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			isShooting = false
		end
	end)
end)

RunService.RenderStepped:Connect(function()
	if isShooting and not shotCooldown then
		shotCooldown = true
		shootEvent:FireServer(character)
		wait(shotDelay)
		shotCooldown = false
	end
end)

Server Script:

local gunTool = script.Parent
local handle = gunTool:WaitForChild("Handle")
local effectsAttachment = handle:WaitForChild("Attachment")

local maxAmmo = 17
local currentAmmo = Instance.new("NumberValue")
currentAmmo.Name = "CurrentAmmo"
currentAmmo.Parent = gunTool
currentAmmo.Value = maxAmmo

local shootSound = handle.Shoot
local emptySound = handle.Empty
local reloadSound = handle.Reload

local shootEvent = gunTool.ShootEvent
local reloadEvent = gunTool.ReloadEvent

reloadEvent.OnServerEvent:Connect(function(player, character)
	reloadSound:Play()
	
	wait(reloadSound.TimeLength)
	
	currentAmmo.Value = maxAmmo
end)

shootEvent.OnServerEvent:Connect(function(player, character)	
	
	local humanoid = character:WaitForChild("Humanoid")
	local recoilTrack = humanoid:LoadAnimation(handle.Recoil)
	
	if currentAmmo.Value > 0 then
		print("shot")
		shootSound:Play()
		currentAmmo.Value = currentAmmo.Value - 1
		
		for _, effects in pairs(effectsAttachment:GetChildren()) do
			if effects:IsA("ParticleEmitter") or effects:IsA("PointLight") then
				effects.Enabled = true
				wait(0.1)
				effects.Enabled = false
			end
		end
		
		recoilTrack:Play()
		
	else
		emptySound:Play()
	end
end)