I've been working on a inventory/weapon script for quite some time and recently i was trying to ### in a burst fire mode for it. It worked for the most part but was buggy if you spammed. Attempting to fix this only made my situation worse causing the game to crash. I know what code is causing the game to crash I just don't know why.
P.S., I'm not using a Tool
===============Code===============
local CanFire = true
local BurstCount = 5
local function Fire(Weapon)
if Weapon.Settings.CurrentMagSize.Value > 0 then
CanFire = true
local Effects = Weapon:WaitForChild("Effects")
if not Weapon.Model.Barrel:FindFirstChild("Smoke") then
_G.Smoke = Effects.Smoke:Clone()
_G.Smoke.Parent = Weapon.Model.Barrel
repeat
if CanFire == true then
Weapon.Settings.CurrentMagSize.Value = Weapon.Settings.CurrentMagSize.Value - 1
BurstCount = BurstCount - 1
_G.Smoke.Enabled = true
wait()
_G.Smoke.Enabled = false
wait(0.05)
CanFire = false
end
until BurstCount == 0
wait(0.5)
CanFire = true
BurstCount = 5
else
repeat
if CanFire == true then
Weapon.Settings.CurrentMagSize.Value = Weapon.Settings.CurrentMagSize.Value - 1
BurstCount = BurstCount - 1
_G.Smoke.Enabled = true
wait()
_G.Smoke.Enabled = false
wait(0.05)
CanFire = false
end
until BurstCount == 0
wait(0.5)
CanFire = true
BurstCount = 5
end
elseif Weapon.Settings.CurrentMagSize.Value <= 0 then
CanFire = false
Reload(player.Character.Inventory:FindFirstChild(currentWeapon.Name))
wait(1)
CanFire = true
end
end
=============================================
Should I use a different type of loop? |