So I have a gun with an ammo counter as a variable but problem is, when I reload and put away my gun once I pull it out back again and shoot it will take double amount of ammo which means you can do that until you get a 1 shot weapon.
I am not very sure of what I should do here if anyone can help.
function Fire(MousePos)
local BarrelPos = Barrel.Position
for _,v in pairs(Barrel:GetChildren()) do
if v:IsA('ParticleEmitter') then
v.Enabled = true
end
end
delay(.02,function()
for _,v in pairs(Barrel:GetChildren()) do
if v:IsA('ParticleEmitter') then
v.Enabled = false
end
end
end)
local mPos = ReturnPosFromRay(MousePos) + Vector3.new(math.random(-Spread, Spread),math.random(-Spread, Spread),math.random(-Spread, Spread))
local B = BulletBase:clone()
if IsLaser then
B.ParticleEmitter.Color = ColorSequence.new(RayColor)
B.ParticleEmitter:Emit(100)
end
local StartCF = CFrame.new(BarrelPos, mPos) * CFrame.new(0, 0, -2)
B.CFrame = StartCF
B.Parent = RayStorageModel
for _, light in pairs(Barrel:GetChildren()) do
if light:IsA("Light") then
Flash(light)
end
end
if FireSound then
SoundEvent:FireServer(FireSound)
end
RenderEvent:FireServer{BarrelPos, mPos, SettingsModule, IgnoreTable}
spawn(function()
recoil = 10
local LastStep = BarrelPos
local DistanceTravelled = 0
while B and DistanceTravelled < 150 do
local Delta = RS:wait()
local CurDistanceTravelled = Delta * Speed
B.CFrame = B.CFrame * CFrame.new(0, 0, -DistanceTravelled) * (IsLaser and CFrame.Angles(0, 0, 0) or CFrame.Angles(math.rad(-.1), 0, 0))
local part, position, surfacenormal = workspace:FindPartOnRayWithIgnoreList(Ray.new(LastStep, (B.Position - LastStep)), IgnoreTable)
if part then
if part.Name == "reflect" and IsLaser then
local reflectpos = reflect(position - LastStep, surfacenormal)
B.CFrame = CFrame.new(position, position + reflectpos,-recoil,recoil)
else
local Humanoid = RecursiveFindHumanoid(part)
if Humanoid then
if Humanoid.Parent ~= part.Parent then
if not IsLaser then
end
end
local OtherPlayer = game.Players:GetPlayerFromCharacter(Humanoid.Parent)
if TKEnabled or (OtherPlayer and OtherPlayer.TeamColor ~= Player.TeamColor or Player.Neutral) or not OtherPlayer then
DamageEvent:FireServer(Humanoid, BaseDamage)
end
end
if ExplosionEnabled then
ExplodeEvent:FireServer(position, ExplosionRadius, BaseDamage, TKEnabled)
end
break
end
end
DistanceTravelled = DistanceTravelled + CurDistanceTravelled
LastStep = B.Position
end
B:Destroy()
end)
end
The shoot code
function Reload()
Reloading = true
UpdateGUI()
if ReloadSound then
SoundEvent:FireServer(ReloadSound)
end
CanFire = false
wait(ReloadTime)
CurrentAmmo = ClipSize
UpdateGUI()
Reloading = false
if Holster == false then
CanFire = true
else
CanFire = false
end
end
So I don’t see anything wrong with the code you have there, but I think I still have an idea of what the problem is. From what I understood, when you unequip and re-equip the weapon, firing it takes twice as much ammo as it should. My guess is that you are connecting the mouse Button1Down event inside the function called when the weapon is equipped, so that each time you equip the weapon, a new connection is made. Could I see the onEquipped function or whatever your equivalent is?
function OnEquipped()
Mouse.Icon = 'http://www.roblox.com/asset/?id=919035852'
Equipped = true
HoldAnim:Play()
UpdateGUI()
Mouse.Button1Down:connect(function()
if Equipped then
MouseDown = true
Mouse.Button1Up:connect(function() MouseDown = false end)
if FireMode:lower() == "semi" then
if Humanoid.Health > 0 and Equipped and CanFire and not Reloading and CurrentAmmo > 0 then
ShootGun()
end
elseif FireMode:lower() == "auto" then
while MouseDown and Humanoid.Health > 0 and Equipped and CanFire and not Reloading and CurrentAmmo > 0 do
ShootGun()
end
end
if CurrentAmmo <= 0 and not Reloading then
Reload()
end
end
end)
As I suspected. Remove the entire Mouse.Button1Down:connect(…) block from the OnEquipped() function and put it in the general area and hopefully that should fix it.