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

Adding that certain line of code lags the game?

game:GetService('RunService').Stepped:Connect(function()
wait()

for i,v in pairs(workspace:GetDescendants()) do  --when i add this part in
  --whatever here doesnt matter

end
end)

3 comments

0

basically; im trying to check if something with a certain name is in the workspace somewhere but doing it this way lags so much

0

well you are sort of indexing every single object in the workspace every however often Stepped goes, (which might be a really small number)

0

how about try a while true do wait (1 (or so)) script instead?

2 answers

✓ Accepted answer

Instead of spam checking the whole workspace to see if anything was added, you can instead use the .DescendantAdded event, which is much more efficient.

Here’s how your code would look like with the event :

game.Workspace.DescendantAdded:Connect(function(instance) --Not sure if just workspace.DescendantAdded would work so I put game.Workspace instead
    if instance.Name == StringToMatchHere then
        --Stuff happens
    end
end)

What makes this easier is that the event would return the instance that was added, so you don’t need to go use a for loop to search through what was added.

Hope this helped!

3 comments

0

yeah, this is better than my script do this

0

both work.. idk who to accept

0

This is more efficient.

x = 1	--or whatever you want but not too small
while true do
wait(x)
 
for i,v in pairs(workspace:GetDescendants()) do  
  --whatever here doesn't matter
 
end
end

Now you aren't doing loads of stuff every frame only every second

EDIT: Denny9876's answer is better than my one

1 comment

0

Accept answer if this helps :D

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