Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Adding that certain line of code lags the game?

Asked by 5 years ago
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)
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 ATestAccount420 31 — 5y
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) OBenjOne 190 — 5y
0
how about try a while true do wait (1 (or so)) script instead? OBenjOne 190 — 5y

2 answers

Log in to vote
3
Answered by 5 years ago
Edited 5 years ago

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!

0
yeah, this is better than my script do this OBenjOne 190 — 5y
0
both work.. idk who to accept ATestAccount420 31 — 5y
0
This is more efficient. CorruptScript 86 — 5y
Ad
Log in to vote
0
Answered by
OBenjOne 190
5 years ago
Edited 5 years ago
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

0
Accept answer if this helps :D OBenjOne 190 — 5y

Answer this question