This is the main script for a gun I'm working on. Some part of the script isn't functioning correctly and I've checked what I can. If the player attempts to shoot(left click) while the reload is occurring, the script breaks and the game crashes. I have no clue what area the problem is in so I included the entire script. Somebody please help this script is essential to the game.

--//Variables
local plr = game.Players.LocalPlayer
local tool = script.Parent.Parent
local hole = tool:WaitForChild("Hole")
local handle = tool:WaitForChild("Handle")
local debounce = true
local config = tool:WaitForChild("Config")
local range = config:WaitForChild("Range")
local dmg = config:WaitForChild("Damage")
local coolDown = config:WaitForChild("CoolDown")
local clips = config:WaitForChild("Clips")
local ammo = config:WaitForChild("Ammo")
local maxAmmo = config:WaitForChild("MaxAmmo")
local allowTracing = config:WaitForChild("AllowTracing")
local reloadTime = config:WaitForChild("ReloadTime")
local isReloading = false
local mouseDown = false
--//Sounds
local reloadSound = handle:WaitForChild("ReloadSound")
local fireSound = handle:WaitForChild("FireSound")
local equipSound = handle:WaitForChild("EquipSound")
local emptySound = handle:WaitForChild("EmptySound")

--//Animations
repeat wait() until plr.Character.Humanoid
local AnimFolder = script.Parent.Parent:WaitForChild("Animations")
local ReloadTrack = plr.Character.Humanoid:LoadAnimation(AnimFolder:WaitForChild("PistolReload"))
local HoldTrack = plr.Character.Humanoid:LoadAnimation(AnimFolder:WaitForChild("PistolHold"))
HoldTrack.Looped = true
 
--//Events
tool.Equipped:connect(function(mouse)
    equipSound:Play()
    HoldTrack:Play()
    mouse.KeyDown:connect(function(Key)
        if Key:lower() == "q" then
            workspace.CurrentCamera.FieldOfView = 60
        end
    end)
   
    mouse.Button1Up:connect(function()
        mouseDown = false
    end)
   
    mouse.Button1Down:connect(function()
        mouseDown = true
        repeat
            if debounce then
                Shoot()
            else
                break
            end
        until mouseDown == false
    end)
   
    --//Reload Event
    mouse.KeyDown:connect(function(key)
        if key:lower() == "r" then
            if not isReloading then
                if clips.Value > 0 then
					mouseDown = false
                    ReloadTrack:Play()
                    reloadSound:Play()
                    isReloading = true
                    ammo.Value = maxAmmo.Value
                    clips.Value = clips.Value - 1
                    debounce = false
                    wait(reloadTime.Value)
                    isReloading = false
                    debounce = true
                end
            end
        end
    end)
   
    function Shoot()
        if not isReloading then
            if ammo.Value > 0 then
                if debounce then
                    --//Play sound
                    fireSound:Play()
                    --//Doesn't allow spamming
                    debounce = false
                    --//Ray Get, Set
                    local ray = Ray.new(hole.CFrame.p, (mouse.Hit.p - hole.CFrame.p) * range.Value)
                    local hit, position = workspace:FindPartOnRay(ray, plr.Character, false, true)
                   
                    --//Bullet Tracing
                    if allowTracing.Value == true then
                        --//Make part
                        local trace = Instance.new("Part", workspace)
                        trace.Material = Enum.Material.Neon
                        trace.BrickColor = BrickColor.new("Black")
                        trace.CanCollide = false
                        trace.Anchored = true
                        trace.Transparency = 0.5
                       
                        --//Show Direction
                        local distance = (hole.CFrame.p - position).magnitude
                        trace.Size = Vector3.new(0.2, 0.2, distance)
                        trace.CFrame = CFrame.new(hole.CFrame.p, position) * CFrame.new(0, 0, -distance/2)
                       
                        --//Remove debris
                        game:GetService("Debris"):AddItem(trace, 0.1)
                    end
                           
                    --//Hit Detection
                    if hit then
                        local humanoid =  hit.Parent:FindFirstChild("Humanoid")
                        if humanoid then
                            if hit.Name == "Head" then
                                --//Double damage on headshots
                                humanoid:TakeDamage(dmg.Value*2)
                            else
                                --//Normal Damage on body shots
                                humanoid:TakeDamage(dmg.Value)
                            end
                        end
                    end
                    ammo.Value = ammo.Value - 1
					wait(coolDown.Value)
                    debounce = true
                end
            else
                --//Out of ammo
                emptySound:Play()
				debounce = false
            end
        end    
    end
end)
 
tool.Unequipped:connect(function()
    mouseDown = false
    HoldTrack:Stop()
    ReloadTrack:Stop()
end)

1 comment

0

Could you make your own script instead of copying SkeleDotLua's?