[Solved] How to fix Automatic gun script not reloading when empty?

I would like to fix a problem with my Automatic Gun script not reloading every time I run out of ammo and make it where my gun will still reload when it is empty instead of reloading only working when gun is partially empty.

When I shoot the gun and it runs out of ammo the reload part of the script breaks. When I run out of ammo the gun stops working and can’t be reloaded or shoot. However, When I shoot halfway the gun allows me to reload and I can shoot some more. The problem seems to occur only when the gun is empty.

I tried almost everything. I asked my friends for help, and they couldn’t help me fix the issue. I also surfed the internet to find solutions, but I couldn’t find anything to solve my problem. I even watched YouTube videos and all I found was reload animations and nothing related to my script.

LocalScript located inside of tool called Fully-Automatic Pistol and sounds are located in the Handle of the gun.

--Object and Sound Variables
local gun = script.Parent
local gun_sound = gun.Handle['Gun Shot']
local empty_sound = gun.Handle.clip_empty
local reload_sound = gun.Handle.Reload
local player = game.Players.LocalPlayer
local clipSize = gun:WaitForChild('Ammo').Value
local ammo = gun:WaitForChild('Ammo')
local Draw_sound = gun.Handle.Gun_Draw
local humanoid = player.Character:WaitForChild('Humanoid')
local Character = player.Character or player.CharacterAdded:Wait()
local holdster_sound = gun.Handle.Gun_holdster

--Gun Animations

local idle = Instance.new("Animation")
idle.AnimationId = "http://www.roblox.com/asset/?id=8391069436"
local pAnim1 = humanoid:LoadAnimation(idle)

local Shoot = Instance.new("Animation")
Shoot.AnimationId = "http://www.roblox.com/asset/?id=8391118897"
local pAnim2 = humanoid:LoadAnimation(Shoot)

--Shooting Cooldown

local cooldown = false

--UserInputService Setup
local userInput = game:GetService("UserInputService")

--Mouse Icon
local mouse = game.Players.LocalPlayer:GetMouse()

mouse.Icon = 'rbxassetid://7905983439'

--Remote Event Setup

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("ShotEvent")
local ammoEvent = ReplicatedStorage:WaitForChild("AmmoPickup")

-- Update ammo function

local function update_ammo()
	player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo: ' .. tostring(ammo.Value) .. '/' .. tostring(gun.MaxAmmo.Value)
end

-- Is gun equipped?


gun.Equipped:Connect(function(mouse)
	player.PlayerGui.ScreenGui.Ammo.Visible = true
	update_ammo()
	Draw_sound:Play()

	pAnim1.Priority = Enum.AnimationPriority.Action
	pAnim1.Looped = true
	pAnim1:Play()
	
-- Is Key R pressed to reload?
	
	userInput.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 then
					if gun.Ammo.Value < clipSize and gun.MaxAmmo.Value > 0 then
						gun_sound.Ended:Wait()
						reload_sound:Play()
						reload_sound.Ended:Wait()
						if gun.MaxAmmo.Value - (clipSize - gun.Ammo.Value) >= 0 then
							gun.MaxAmmo.Value = gun.MaxAmmo.Value - (clipSize - gun.Ammo.Value)
							gun.Ammo.Value = clipSize
						else
							gun.Ammo.Value = gun.Ammo.Value + gun.MaxAmmo.Value
							gun.MaxAmmo.Value = 0
							update_ammo()
						end
					end
				end
			end
		end
	end)
	
-- Is Left Mouse key pressed to shoot and is Player currently shooting?
	
	local shooting = false
	
	mouse.Button1Down:Connect(function()
			shooting = true
		while shooting do
			if gun.Ammo.Value > 0 and cooldown == false then
			wait(0.1)
			cooldown = true
			remoteEvent:FireServer(gun.Handle.Position, gun.Handle.Orientation, mouse.Hit.p)
			gun_sound:Play()
			pAnim2:Play()
			gun.Ammo.Value = gun.Ammo.Value - 1
			wait(0.145)
				cooldown = false
			else
				empty_sound:Play()
			
			end
			end
	end)
	
	-- Stop shooting when Player releases Mouse Button
	
	mouse.Button1Up:Connect(function()
			shooting = false
	end)
	
	-- Is Player holding Right Mouse key to aim?
	
	mouse.Button2Down:Connect(function()

		local camera = game.Workspace.CurrentCamera

		local DefaultFoV = 70

		local Properties = {FieldOfView = DefaultFoV - 30}
		local Info = TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut) -- edit as you want
		local T = game:GetService("TweenService"):Create(camera, Info, Properties)
		T:Play()
	end)
	
	-- Return player to normal camera view once Right Mouse key is released.
	
	mouse.Button2Up:Connect(function()

		local camera = game.Workspace.CurrentCamera

		local DefaultFoV = 40

		local Properties = {FieldOfView = DefaultFoV + 30}
		local Info = TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut) -- edit as you want
		local T = game:GetService("TweenService"):Create(camera, Info, Properties)
		T:Play()
	end)
end)

--Unequip Gun

gun.Unequipped:Connect(function()
	player.PlayerGui.ScreenGui.Ammo.Visible = false
	holdster_sound:Play()
	pAnim1:Stop()
	pAnim2:Stop()
end)

-- Did ammo amount change when player fired bullet? If so update player's ammo for current gun.

ammo.Changed:Connect(update_ammo)

ammoEvent.OnClientEvent:Connect(function()

	gun.MaxAmmo.Value = gun.MaxAmmo.Value + 10
	update_ammo()

end)

Note This script uses Remotes, and their locations are below:

image

1 Like

In the while shooting do part of the script, the while loop only waits when the gun still has ammo, so when it runs out the while loop runs infinitely fast and crashes the game

How can I fix that? Do I need to use the repeat function?

Add wait() under empty_sound:Play() and that should stop it from looping infinitely fast

1 Like

Ok, That works but now it won’t reload.

Should I update the reload part of the script to check if the shooting is equaled to false?

Here is a video to show you what I mean about not being able to reload:

robloxapp-20220102-2325070.wmv (778.9 KB)

break instead of wait() should stop the loop because the gun Is already empty.

I will try that. Thank you! I will let you know if it works.

It doesn’t work. Sorry though.

I think you could try changing gun.Ammo.Value < clipSize to gun.Ammo.Value < gun.MaxAmmo.Value in the reload function

The wait() worked better. I will add the wait() back but I need to fix the reloading part.

I will try that as well. I will let you know if that is working correctly.

Do I keep the > 0 part at the end of both of those statements?

If you wish to, sure. That part only stops the reload if the gun shouldn’t have any ammo to begin with.

If I do gun.Ammo.Value < gun.MaxAmmo.Value > 0 I get this error:

Players.AEW745.Backpack.Fully-Automatic Pistol.Fully-Auto Pistol:67: attempt to compare number < boolean

Which line is line 67 in the script

This is line 67 in the script.

Also I think you should make MaxAmmo into a value that isn’t manipulated, meaning that it stays constant and indicates the maximum clip ammo. So the script should be:

-- Checks that the ammo the gun currently has is < than the maximum ammo in the clip
if gun.Ammo.Value < gun.MaxAmmo.Value and gun.MaxAmmo.Value > 0 then
    gun_sound.Ended:Wait()
    reload_sound:Play()
    reload_sound.Ended:Wait()
    -- Sets the Ammo in the clip to the maximum allowed ammo in the gun
    gun.Ammo.Value = gun.MaxAmmo.Value
    
    update_ammo()
end

I do have a value. It checks the value of the MaxAmmo inside of the gun look below:

image