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: