Getting Descendants is acting wierd

So I’m working on a script that controls a beam spell. I’m using a module that detects wether a part is in a ray it will return true if it finds the part, but when I get the descendants of workspace it causes a HUGE lag wave. I don’t know if I should use a different method. Here is the code.

1 Like

so you get the amount od descendents in workspace, then for the number you get all the descendants like another 5+ times, so you could be calling :GetDescendants thousands of times.

yes, yes you should, try something like

local descendants = workspace:GetDescendants()
for i, v in pairs(descendants) do
    --whereveryou have game.Workspace:GetDescendants()[e], change that to v. for example 
end

change

if game.Workspace:GetDescendants()[e].Parent then
--to
if v.Parent then
1 Like

Thank you so much! It really helped out.