This code makes studio freeze, and I have no idea why

So, this piece of code makes studio freeze: (I added a comment to the for loop that freezes studio, without it it works fine.)


	local CurrentRadius = 0
	local Speed = 343
	
	local PartsWithinMaxDistance = workspace:GetPartBoundsInRadius(Blast.Position, MaxDistance)
	
	local HeatDamageDist = (MaxDistance / 100)
	local FireBlastDamageDist = (MaxDistance / 30)
	local BlastDamageDist = (MaxDistance / 5)
	
	while CurrentRadius <= MaxDistance do
------------------------------------------------------------
		local Intensity = 1/CurrentRadius^2
		
		for _, Player in next, Players:GetPlayers() do
			if Player:DistanceFromCharacter(Blast.Position) <= CurrentRadius and Player.Character then
				if Player.Character.Humanoid.Health > 0 then
					NuclearBlastEvent:FireClient(Player)
					Player.Character.Humanoid:TakeDamage(100)
				end
			end
		end
		
                --this for loop freezes studio
		for _, obj in next, PartsWithinMaxDistance do
			local Distance = (Blast.Position - obj.Position).Magnitude
			
			if obj.Locked == false and obj:GetAttribute("NuclearBlast_HIT") == false and Distance <= MaxDistance then
				obj:SetAttribute("NuclearBlast_HIT", true)
				
				if Distance < HeatDamageDist then
					obj:Destroy()
					
					print("Heat Damage")
				elseif Distance >= HeatDamageDist and Distance < FireBlastDamageDist then
					if obj.Material == Enum.Material.Glass then 
						obj:Destroy() 
					else
						obj.Anchored = false
						
						--Instance.new("Fire", obj).Size = 50	
						
						print("Blast Damage (FIRE)")
					end
				elseif Distance >= FireBlastDamageDist and Distance < BlastDamageDist then
					if obj.Material == Enum.Material.Glass then 
						obj:Destroy() 
					else
						obj.Anchored = false
						
						print("Blast Damage (NO FIRE)")
					end
				elseif Distance >= BlastDamageDist and obj.Material == Enum.Material.Glass then
					obj:Destroy()
					
					print("Glass Damage")
				end
			end
			
			RunService.Heartbeat:Wait()
		end
		
		Mesh.Scale += Vector3.new(Speed, Speed, Speed) --Vector3.new(Speed, 0, Speed)
		CurrentRadius += Speed
------------------------------------------------------------
		RunService.Heartbeat:Wait()
	end

It is because of the loop. If you add a wait() within your loop it should fix ur issue. It is cuz the loop goes extermaly fast so you need to have a wait there to slow it down a bit.

If you didn’t see, I added RunService.Heartbeat:Wait().

I don’t think that works though in the same way. Try the wait I bet it will work fine then.

Terribly sorry if I’m forgetting something for optimization reasons, but why not just do :Connect() instead of :Wait() so you are not using a While loop for code that needs to execute so fast.

And reading over your code I can only assume there is better ways in general to write this that shouldn’t require you to be using a loop.

Good point, but still, it works fine without that one for loop, and with it it freezes studio.

Yes, well that loop is a very expensive to be running at the rate you want it to be running at. It’s something you will need to either slow down or simplify.

Consider looking into other event triggers and ditch the need for a while loop.

Coming back on this issue, I have sort of rewritten the code, and it still crashes studio:

	local CurrentRadius = 0
	local Speed = 343
	local MaxDistance = 500000
	
	local PartsWithinMaxDistance = workspace:GetPartBoundsInRadius(Blast.Position, MaxDistance)
	local HDamageDist, FBDamageDist, BDDist = (MaxDistance / 100), (MaxDistance / 30), (MaxDistance / 5) -- HeatDamage, FireBlastDamage, BlastDamage
	
	local Loop
	
	Loop = RunService.Heartbeat:Connect(function(dt)
		if CurrentRadius > MaxDistance and Loop then print("Blast Ended") Loop:Disconnect() return end
		
		Mesh.Scale += Vector3.new(Speed, Speed, Speed)
		CurrentRadius += Speed
		
		local Intensity = 1/CurrentRadius^2
		
		for _, Part in next, PartsWithinMaxDistance do
			-- Player Damage --
			local Humanoid = Part.Parent:FindFirstChildOfClass("Humanoid")
			if Humanoid and Humanoid.Health > 0 then
				Humanoid:TakeDamage(Intensity)
			end
			-- ------------- --
			
			-- Blast Damage --
			if not Part.Locked and Part.CanCollide == true then
				
				local DistanceFromCenter = (Blast.Position - Part.Position).Magnitude
				
				if DistanceFromCenter <= HDamageDist then -- Everything is gone within this radius.
					Part:Destroy()
					print("Heat Damage")
				elseif DistanceFromCenter > HDamageDist and DistanceFromCenter <= FBDamageDist then -- Everything will be flung and set on fire within this radius.
					if Part.Material == Enum.Material.Glass then Part:Destroy() return end
					
					Part.Anchored = false
					FireParticles:Clone().Parent = Part
					print("Fire and Blast Damage")
				elseif DistanceFromCenter > FBDamageDist and DistanceFromCenter <= BDDist then -- Everything will be flung within this radius.
					if Part.Material == Enum.Material.Glass then Part:Destroy() return end
					
					Part.Anchored = false
					print("Blast Damage")
				elseif DistanceFromCenter > BDDist then -- Glass will be gone within this radius.
					if Part.Material == Enum.Material.Glass then
						Part:Destroy()
						print("Glass Damage")
					end
				end
				
			end
			-- ------------ --
			
			
			
			task.wait(1)
		end
	end)

Found the problem, it was workspace:GetPartBoundsInRadius crashing studio, nice job roblox.

The issue is that your max distance is too high. Using a smaller value will not crash, but because the operation is computationally expensive, the high value causes the program to hang.

I need high values though, ill try another way.