I have a gun, and it has an ammo value. Everytime I shoot the gun, it works fine.. I have the max amount of ammo (15). It shoots it all, then I reload and shoot it again, still, everything works.
But when I unequipt my gun, then I take it back, suddenly it shoots 8 bullets instead. And its like the ammo value changed from 15 to 8. The script tells the gun to lose only 1 bullet PER SHOT. But for some reason it loses way more ammo.
I tried making a ScriptModule to store the ammo there, same issue. I tried making a Value inside the gun script to store the ammo there, same issue. I tried making a Value inside the gun it senf (NumberValue) to store the ammo there, same issue.
LOCAL SCRIPT (Mostly the input framework, to send a remote events to the server script)
local shootremote = script.Parent:WaitForChild("ShootRemote")
local reloadremote = script.Parent:WaitForChild("ReloadRemote")
out = false
script.Parent.Equipped:Connect(function()
out = true
idle:Play() --this is an animation ignore it
mouse.Button1Down:connect(function()
shootremote:FireServer()
end)
end)
script.Parent.Unequipped:Connect(function()
out = false
idle:Stop() --this is an animation ignore it
end)
uis.InputBegan:connect(function(key)
if key.KeyCode == Enum.KeyCode.R then
if out == true then
reloadremote:FireServer()
end
end
end)
SERVER SCRIPT (The script that handles everything else, shooting, effects, reload.)
local ammo = script.Parent.Ammo.Value -- These both are the value of a number value inside the gun, this is the last method I tried to use, but as I said, didnt work, like the other 2 methods.
local clip = script.Parent.Clip.Value
function shoot(player)
local char = player.Character
local hum = char.Humanoid
local came = game.Workspace.Camera
local recoilanim = hum:LoadAnimation(anims.recoil)
if ammo > 0 then
main.Fire:Play()
main.Flash.Enabled = true
main.PointLight.Enabled = true
recoilanim:Play()
ammo = ammo - 1 --Here it says to lose ONLY ONE ammo.
wait(0.1)
main.Flash.Enabled = false
main.PointLight.Enabled = false
recoilanim:Stop()
elseif ammo == 0 and reloading == false then
main.Parent.Handle.empty:Play()
end
end
reloading = false
function reload(player)
local char = player.Character
local hum = char:WaitForChild("Humanoid")
local reloadanim = hum:LoadAnimation(anims.reload)
if ammo == 0 and reloading == false then
reloading = true
reloadanim:Play()
reloadanim:AdjustSpeed(1.2)
wait(1)
main.magin:Play()
wait(0.5)
reloadanim:Stop()
wait(0.2)
main.Parent.Handle.boltclose:Play()
ammo = clip
reloading = false
elseif ammo < clip and reloading == false then
reloading = true
reloadanim:Play()
reloadanim:AdjustSpeed(1.2)
wait(1)
main.magin:Play()
wait(0.5)
reloadanim:Stop()
wait(0.2)
ammo = clip
reloading = false
end
end
shootremote.OnServerEvent:Connect(function(plr)
shoot(plr)
end)
reloadremote.OnServerEvent:Connect(function(plr)
reload(plr)
end)

5 comments
use the Activated event instead of nesting Button1Down inside Equipped
Nope, still losing too much ammo.
wasnt a fix just a suggestion
When you have used :connect(), please try using :Connect() it may not fix it but will prevent bugs in the near future.
Oh, alright, thank you. :D