Roblox studio crashing with raycasting?

  1. What do you want to achieve?
    Make the rays updating accordingly and prevent studio from crashing

  2. What is the issue?
    Studio
    https://gyazo.com/3042b42e2e496d6894f58f4c31c95023 ← X.1 size avg size I wish to use it like this
    https://gyazo.com/e322d7755cfe422cad88eb447489153a ← X25 size, appears it doesn’t crash at this size

  3. What solutions have you tried so far?
    I have made the sphere bigger which slowed down the script and worked but making it smaller and attempting to slow it down with waits resulted it to crash, removing the code to update the ray table while its small also prevents it from crashing

This script creates a sphere with the num points, also highlights some of them
https://gyazo.com/6e302f4c0a176c1ae95a28a03efc43a8 ← example of sphere at X25 size
After the sphere is created it loops over each part and shoots out a ray (360 rays/1 per part)
https://gyazo.com/e322d7755cfe422cad88eb447489153a ← sphere at X25 size but now shooting rays
I don’t know what else to write here if you don’t understand parts of my poorly written script just ask

local numPoints = 360 -- number of points on the sphere
local turnFraction = 0.61803 -- golden ratio
local defaultcolor = BrickColor.new("Royal purple") -- default cphere clr
local highlight = 2
local highlightoffset = 0
local sspawn = game.Workspace.CircleSpawn
local PS = sspawn.Position -- point where to spawn 
sspawn:Destroy()
local colsraysize = 25

local function VisualizeRay(part, pos, clr, mod, name) -- visualize ray, part/pos = used for cframe, clr = used for changing color, mod = modifier for the ray size, name = rays name
	local RayVisualize = Instance.new("Part")
	RayVisualize.CanCollide = false
	RayVisualize.Anchored = true
	RayVisualize.Name = name
	RayVisualize.Size = Vector3.new(.25, .25, colsraysize + mod)
	RayVisualize.Color = clr
	RayVisualize.CFrame = CFrame.new(part.CFrame.p, pos) * CFrame.new(0, 0, -(colsraysize + mod) / 2)
	RayVisualize.Parent = game.Workspace.BirdRays
end

local function PlotPoint(x, y, z, clr) -- create a ball at X/Y/Z then look at center
	local new = Instance.new("Part")
	new.Shape = "Ball"
	new.Parent = game.Workspace.circle
	new.Anchored = true
	new.Size = Vector3.new(6.5, 6.5, 6.5)
	new.Position = PS + Vector3.new(x, y, z)
	new.CFrame = CFrame.new(new.Position, Vector3.new(PS.X, PS.Y, PS.Z))
	new.BrickColor = clr
	return new
end

local function IgnoreList()
	local il = {}
	for i,v in pairs(game.Workspace.BirdRays:GetChildren()) do
		table.insert(il, v)
	end
	for i,v in pairs(game.Workspace.circle:GetChildren()) do
		table.insert(il, v)
	end
	return il
end

for i=0, numPoints, 1 do -- this creates the actually sphere https://stackoverflow.com/questions/9600801/evenly-distributing-n-points-on-a-sphere/44164075#44164075
	spawn(function()
		local t = i / (numPoints-1)
		local inclination = math.acos(1 - 2 * t)
		local azimuth = 2 * math.pi * turnFraction * i
		local angle = 2 * (math.pi * (turnFraction * i))
		
		local x = math.sin(inclination) * math.cos(azimuth) * .1
		local y = math.sin(inclination) * math.sin(azimuth) * .1
		local z = math.cos(inclination) * .1
		
		local color = defaultcolor
		if ((i+highlightoffset) % highlight == 0) then
			color = BrickColor.new("Gold")
		end
		local thing = PlotPoint(x, y, z, color)
		
	end)
	
end

wait(.01) -- make sure sphere is made
spawn(function()
	while true do
		game.Workspace.BirdRays:ClearAllChildren() -- remove all rays
		for i,v in pairs(game.Workspace.circle:GetChildren()) do
			spawn(function()
				local il = IgnoreList()
				local newray = Ray.new(v.Position, -v.CFrame.lookVector*(colsraysize)) -- invert direction of ray so its firing away from the sphere
				local hit, pos = game.Workspace:FindPartOnRayWithIgnoreList(newray, il)
				if hit and i <= 270 then -- the 270 is for a future script that will be using this 
					VisualizeRay(v, pos, Color3.fromRGB(0, 255, 0), 0, i) -- green if hit
				elseif hit and i > 270 then
					VisualizeRay(v, pos, Color3.fromRGB(0, 0, 0), 0, i) -- black if hit but out of FOV
				elseif i <= 270 then
					VisualizeRay(v, pos, Color3.fromRGB(0, 0, 255), 0, i) -- blue if in FOV
				else
					VisualizeRay(v, pos, Color3.fromRGB(255, 0, 0), 0, i) -- red if out of FOV
				end
			end)
		end
		wait()
	end
end)

If you know how i can improve this please reply

You’re creating tons of threads and then looping each one indefinitly without enough of a pause. I think you may be overloading it.

i did this to hopefully speed it up, removing the spawns and replacing it with a wait() dramatically slows it down
https://gyazo.com/413e80d5f4c0097b01559ebd0eed4d6c

How many threads were you originally making?

i am not quite sure but it was 1 thread per part, with 360 parts so i see what you mean
Edit:
This is what i am attempting to do: https://gyazo.com/8a10deffc5a9fb11033fe7154f380194

You’re going to want to come up with a more dynamic way of doing this, try using https://draw.io

Try using coroutine.wrap() or coroutines as they have better functionality than spawn()

i tried that but with wrap/create but the threads die do you know how i can fix that? (i cant find out how to prevent it dying)
https://gyazo.com/520695d5cb80a2d110e08dd222113b55
i did try with a while loop but it is a bit slow
https://gyazo.com/1634fa0df0c62a0551d735a6eed904b7
(at the end of ^ this gif i removed the wait() but it froze studio [Script timeout: exhausted allowed execution time] )

There is a way to not to make a thread die, you can just loop it, or add coroutine.yeild()
I would better say you look through these Roblox Wiki articles about Beginners guide on coroutines, Couroutines, and also Couroutine Vs Spawn

coroutines did make my script better thank you, but i found out the main cause of lag was my “visualizeray” function removing it increased my fps but not by 100% (this feature was mainly for debugging)