Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

My computer freezes when the clip runs out of ammo in my weapon, why?

Asked by 4 years ago

This is my code, I can't figure out why it's freezing. If someone could figure out what's wrong, could they tell me what is? I think it might be the firing part because I just added it and this started happening.

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

--//Events
tool.Equipped:Connect(function(mouse)
    equipSound:Play()
    tool.Activated:Connect(function()
    firing = true
    repeat
        if not isReloading then
            if firing == true then
            if ammo.Value > 0 then
                if debounce then
                    --//Play sound
                    fireSound:Play()
                    --//Doesn't allow spamming
                    debounce = true
                    --//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
                       wait()
                        --//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("Zombie")
                        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()
  end          end        
end
--if ammo == 0 then 
--firing = false
--end
mouse.Button1Up:Connect(function()
    wait()
firing = false
end)

until firing == false
    end)
    --//Reload Event
    mouse.Button1Down:Connect(function(click)
        --key:lower()
        --if key == "r" then
            if not isReloading then
                if clips.Value > 0 then
                    if ammo.Value == 0 then
                    isReloading = true
                    reloadSound:Play()
                    wait(reloadTime.Value)
                    ammo.Value = maxAmmo.Value
                    clips.Value = clips.Value - 1
                    isReloading = false
                end
            end
        end
   end)
end)
0
Should I change "until firing == false"? zandefear4 90 — 4y
0
@zandefear4 Do you have a `wait()` in your `until` loop? iSpaceRBLX 31 — 4y
0
Make sure you have waits and most parts unioned, if still crashing that means it's your PC NicoXerocious 29 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

When you run out of ammo, your script does not yield in any way so it runs as fast as possible which ends up freezing your computer.

All I did was add a wait() inside an else statement on line 79.

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

--//Events
tool.Equipped:Connect(function(mouse)
    equipSound:Play()
    tool.Activated:Connect(function()
    firing = true
    repeat
        if not isReloading then
            if firing == true then
            if ammo.Value > 0 then
                if debounce then
                    --//Play sound
                    fireSound:Play()
                    --//Doesn't allow spamming
                    debounce = true
                    --//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
                       wait()
                        --//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("Zombie")
                        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
                wait() --//Added a wait so it yields.
                --//Out of ammo
               -- emptySound:Play()
        end
    end        
end
--if ammo == 0 then 
--firing = false
--end
mouse.Button1Up:Connect(function()
    wait()
firing = false
end)

until firing == false
end)
    --//Reload Event
    mouse.Button1Down:Connect(function(click)
        --key:lower()
        --if key == "r" then
            if not isReloading then
                if clips.Value > 0 then
                    if ammo.Value == 0 then
                    isReloading = true
                    reloadSound:Play()
                    wait(reloadTime.Value)
                    ammo.Value = maxAmmo.Value
                    clips.Value = clips.Value - 1
                    isReloading = false
                end
            end
        end
   end)
end)

This should solve your issue of freezing.

0
Thanks, that fixed it. zandefear4 90 — 4y
0
np CeramicTile 847 — 4y
Ad

Answer this question