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

The MaxAmmo is 15 and the Ammo is 10.

Ohh I see what you mean, MaxAmmo is the maximum ammo the player has. My bad.

I think you could make a 3rd value called CurrentClipAmmo that tells you the current ammo in the current gun clip. If you want to, you could use a system called Instance Attributes so your workspace doesn’t get cluttered with variables, and change the things like Ammo.Value, MaxAmmo.Value, and CurrentClipAmmo.Value in the following script to Tool:GetAttribute("Ammo"), Tool:GetAttribute("MaxAmmo"), and Tool:GetAttribute("CurrentClipAmmo").

The new script would be:

-- Checks that the ammo the gun currently has is less than the maximum ammo in the clip
if Gun.CurrentClipAmmo.Value < Gun.Ammo.Value then
    local NewAmmo = Gun.Ammo.Value - Gun.CurrentClipAmmo.Value
    
    -- If more bullets need to be reloaded then there are left, reload the remaining bullets instead
    if NewAmmo > Gun.MaxAmmo.Value then NewAmmo = Gun.MaxAmmo.Value end 
    
    if NewAmmo ~= 0 then -- Checks to make sure there is ammo to reload
        gun_sound.Ended:Wait()
	    reload_sound:Play()
	    reload_sound.Ended:Wait()

        Gun.MaxAmmo.Value -= NewAmmo -- Removes the loaded bullets from the player's current amount
        Gun.CurrentClipAmmo.Value += NewAmmo -- Adds the ammo to the max clip
        
        update_ammo()
    end
end

If there is an issue with it please let me know

What type of value is the CurrentClipAmmo? Is it an IntValue?

It can be either a NumberValue or an IntValue

Can I still use my key input for this?

What do I put for the Value of the CurrentClipAmmo? I currently have it as 0 and it doesn’t reload still.

Could you post the code that you have now with all the edits

Here is the updated code:

--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
					-- Checks that the ammo the gun currently has is less than the maximum ammo in the clip
					if gun.CurrentClipAmmo.Value < gun.Ammo.Value then
						local NewAmmo = gun.Ammo.Value - gun.CurrentClipAmmo.Value

						-- If more bullets need to be reloaded then there are left, reload the remaining bullets instead
						if NewAmmo > gun.MaxAmmo.Value then NewAmmo = gun.MaxAmmo.Value end 

						if NewAmmo ~= 0 then -- Checks to make sure there is ammo to reload
							gun_sound.Ended:Wait()
							reload_sound:Play()
							reload_sound.Ended:Wait()

							gun.MaxAmmo.Value -= NewAmmo -- Removes the loaded bullets from the player's current amount
							gun.CurrentClipAmmo.Value += NewAmmo -- Adds the ammo to the max clip

							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()
				wait()
			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)

Ah, I forgot some other things. You can either change gun.Ammo.Value in the Mouse.Button1Down function to gun.CurrentClipAmmo.Value, or switch around the values in the reloading function

Still doesn’t work. I still can’t reload the gun.

I had to delete the gun_sound.Ended:Wait() for the gun to work properly.

1 Like