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

How do I fix a randomly crashing loop?

Asked by 9 years ago

Ok, so I have a gun kit that has bullet trails, and the bullet trails sometimes cause the player using the gun to crash.

Here's the loop that creates the bullet trails:

spawn(function()
    local LastPos2 = nil
    while true do
        if LastPos2 then
            if (not Bullet:IsDescendantOf(game)) then break end
            Bullet.CFrame = CFrame.new(Bullet.CFrame.p, Bullet.CFrame.p + Bullet.Velocity)
            local Trail = Instance.new("Part")
            Trail.BrickColor = S.TrailColor
            Trail.Transparency = S.TrailTransparency
            Trail.Anchored = true
            Trail.CanCollide = false
            Trail.Size = VEC3(1, 1, 1)
            local Mesh = Instance.new("BlockMesh")
            Mesh.Offset = VEC3(0, 0, -(Bullet.Position - LastPos2).magnitude / 2)
            Mesh.Scale = VEC3(S.TrailThickness, S.TrailThickness, (Bullet.Position - LastPos2).magnitude)
            Mesh.Parent = Trail
            Trail.Parent = Gun_Ignore
            Trail.CFrame = CF(LastPos2, Bullet.Position)
            delay(S.TrailVisibleTime, function()
                if S.TrailDisappearTime > 0 then
                    local X = 0
                    while true do
                        if X == 90 then break end
                        if (not Selected) then break end
                        local NewX = X + (1.5 / S.TrailDisappearTime)
                        X = (NewX > 90 and 90 or NewX)
                        local Alpha = X / 90
                        Trail.Transparency = NumLerp(S.TrailTransparency, 1, Alpha)
                        RS:wait()
                    end
                    Trail:Destroy()
                else
                    Trail:Destroy()
                end
            end)
            LastPos2 = Bullet.Position
        else
            LastPos2 = Main.CFrame.p
        end
        wait() --There's a wait in this loop
    end
end)

And this loop is located in the Firing function, which is called every time the gun is fired.

This is the loop that fires the gun:

while MB1_Down and (not Reloading) do
    if Knifing and (not ThrowingGrenade) then break end
    if Running then break end
    if Ammo.Value > 0 then
        Ammo.Value = Ammo.Value - 1
        if Humanoid.Health ~= 0 then
            if Aimed and Steady_Key_Pressed and S.UnSteadyOnFire then
                Steady_Key_Pressed = false
                CurrentSteadyTime = 0
            end
            Fire_Gun() --This is the Firing function which the loop is located in
        end
    end
    if Ammo.Value == 0 and S.AutoReload then
        wait(0.2)
        Reload()
    end
    wait(60 / S.FireRate) --There's a wait in this loop
end

The problem is, sometimes the gun randomly crashes after the gun is fired, and I know it's caused by the trails because when I comment it out, the gun doesn't crash. Any ideas on what the problem might be or on how to fix this problem? Thanks.

0
This is caused because you're having an endless loop with no yeild - while true do --code end - You need to puta wait() in those loops. Goulstem 8144 — 9y
0
Both loops have wait()'s though TurboFusion 1821 — 9y
0
I would say its a stack error from ROBLOX with Fire_Gun() because of the spawn function. MessorAdmin 598 — 9y
0
Ok, how would I fix it then? TurboFusion 1821 — 9y

1 answer

Log in to vote
-2
Answered by
X_Z 0
9 years ago

The first loop doesn't has a wait() before the final end

0
The final end is the end to the spawn() function, not the loop TurboFusion 1821 — 9y
Ad

Answer this question