Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I fix a randomly crashing loop?

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.

4 comments

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.

0

Both loops have wait()'s though

0

I would say its a stack error from ROBLOX with Fire_Gun() because of the spawn function.

0

Ok, how would I fix it then?

1 answer

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

1 comment

0

The final end is the end to the spawn() function, not the loop

This discussion is preserved as it was. New answers, comments, and votes are closed.