If you have additions to make to this post to make it answerable for community members here, then please use the “Edit” functionality on your post to add this information.
Likewise, if you have solved the problem, you should mark the helpful post as the solution so that other users will not respond.
Roblox Lua, when interpreting code, will constantly look ahead of itself to stay quick; therefore, when creating a recurring loop like this, you need a wait() time to let the program breathe a little. The base wait() time without notating an amount of time is 0.03 seconds, which would put the run time at about 50 seconds. To combat this, either try reducing the loop count or sparingly reduce the wait time to maybe 0.01 .
If you have to slow down the rate of the loop by making it yield, you don’t necessarily have to yield for every iteration. Loops mostly start to lag when you are getting into the tens of thousands of iterations, so you can make it yield only every 1000 iterations or so, which would make it run much faster than if you are waiting every cycle.
I’ve also done some extra minor optimizations such as moving the variable declaration for p outside of the body of the loop, and replaced y with a formula using i
local rs = game:GetService("RunService")
local p
for i=1,100000 do
p = Instance.new("Part")
p.Anchored = true
p.Position = Vector3.new(0, (i - 1) * 2, 0)
p.Parent = workspace
if (i % 10000 == 0) then
rs.Heartbeat:Wait()
end
end
print("Hello world!")