Hello, I created a script for my pistol by following a YouTube tutorial (- YouTube) And I’ve encountered a big problem. Whenever I holster the firearm, the ammo count drops. For example, at first I’ll have a full mag with 7 bullets. But when I holster and pull it back out, It’ll still claim I have 7, but I can only fire 5 times. This will go down everytime I holster and unholster until I can only fire 1 round per reload.
Sorry if this isn’t very helpful but this is just a straight-up terrible script. It’s connecting a bunch of events every single time you equip the weapon. Notice how all of the weapon’s functions are wrapped inside the Equipped connection’s function.
local maxAmmo = 7
local Ammo = maxAmmo
local reloading = false
local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild(“PlayerGui”)
local textlabel = playerGui:WaitForChild(“AmmoDisplay”):FindFirstChild(“AmmoText”)
script.Parent.Equipped:Connect(function(Mouse)
local function reload()
reloading = true
wait(3)
Ammo = maxAmmo
reloading = false
end
script.Parent.Activated:Connect(function()
if Ammo > 0 and not reloading then
Ammo = Ammo - 1
script.Parent.GunShot:Play()
if Mouse.Target.Parent:FindFirstChild("Humanoid")then
script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 40)
end
elseif reloading == false then
script.Parent.GunReload:Play()
reload()
script.Parent.GunShot:Stop()
end
while wait() do
textlabel.Text = (Ammo).." / "..maxAmmo
end
end)
local input = game:GetService("UserInputService")
input.InputBegan:Connect(function(Key)
if Key.KeyCode == Enum.KeyCode.R and reloading == false and Ammo ~= maxAmmo then
script.Parent.GunReload:Play()
reload()
end
end)