If you guy make a script about whlie true do. So this script will have a effect to make game lag?
It's while true do, not whlie true do.
It doesn't lag if you use it correct
Example for a bad script
while true do -- code end
Example for a good one
while true do wait() -- Code end
But you could also do this
while wait() do
Do not forget these () though.
Oh, and if you don't add these, your studio will crash (with all the examples)
Though, if you make a heavy script with while true do it might still lag
The more frecuent the loop runs, the more memory it will consume, which translates to more lag. It's fine if you have a few loops running frecuently (lets say, at a wait()
frecuency), but try to use longer wait() times, like 1+ seconds.
Hello there! I'm BlackOrange and I will be helping you with your issue!
While True Do
, Does it create lag? Well, I would say a limit for a game could be 5 of them. The thing is, the more While True Do Loops you have, the more laggy and more crashes you will get. Now there is always way to go around this problem. There are certain things people use While True Do
for, such as stat changing (constantly updating stats) or loops in general just to update text.
For Stat Changing, there is something we use to decrease the amount of While True Do
at play. We use .Changed
Now here is a quick example:
local ExampleValue = Instance.new('NumberValue') ExampleValue.Changed:Connect(function() -- code end)
You see how it works? Instead of using while true do and then updating, we could do something like that which is more efficient and less laggy.
Now, some people use While True Do and really need to use it. One way we prevent doing this is using RenderedStepped / RunService (note that this is for LocalScripts)
local RunService = game:GetService('RunService') RunService.Stepped:Connect(function() -- I'm not sure if this is how you use it -- code end)
Now, I'm not so familiar with that up there (sorry) but you could do more research on it
To conclude, having a bunch of never ending codes running is a bad play, you will not only get lag but in some cases crashes and you would have to remove the loop for it to stop.
Hopefully this helped you if you needed other ways to use While True Do and understand it
-- BlackOrange3343