How laggy is while true do

Hey all, I am just wondering how laggy something like a
while true do wait() end
script is. for example, I want to make a shop and when you walking into the room, you are prompted to view the shop and I want to use a region3 to check when they enter the area. how laggy would checking it every wait() make it. or give me some idea of what i should look at

1 Like

I believe there is a function to see if a player has entered a region. There is always a better way than a loop.

Don’t ever use a while true loop. For your case, you can just used .Touched to see when someone enters your area.

Whats the function. Tell me please

I’d actually try the new method Roblox released, and with that said Region3s are deprecated.

I also believe from what I heard to some extent these methods are more performant than Region3s.

Since this is for a shop, locally run this in a Heartbeat connection, rather than a while loop.


me too please I would actually like to know haha

You could use a .Touched event attached to a BasePart brick

Here is a short example script I wrote, this all runs inside a LocalScript

local region = workspace.Shop -- This is an invisible brick that is resized to surround the zone

local db = false -- when the player leaves shop make sure to set this back to false

	if hit.Parent:FindFirstChild("Humanoid") then
		db = true
		local char = hit.Parent
		local plr = game:GetService("Players"):GetPlayerFromCharacter(char)
		-- view shop

The .Touched solution doesn’t implicate any repeating functions, it will only fire when interacted upon by a player. 0 lag.

1 Like

That is an option, but I assume he wants it to toggle visibility when the user exits the shop. I’ve had some problems with the reliability of TouchEnded (I don’t think it works well for non-collidable parts.)


You literally can’t use smooth terrain without region3s, so how are region3’s deprecated?

1 Like

There’s no noticeable benefit to using while true do --code wait() end - or connecting the function to RunService.Heartbeat, for that matter - over putting the same code in a loop that runs at a lower frequency. Checking if characters are in a zone 10 times every second will feel just as responsive to players as checking it 40 to 60 times a second, and will free up resources on the server.

local CHECK_FREQUENCY = 10 --Frequency at which we check for players in the zone, in hertz.

while task.wait(1/CHECK_FREQUENCY) do
  --Check for player characters

However, I’d recommend using a module such as ZonePlus over checking a Region3 10 times a second.

1 Like

Looking at post #107:
Introducing OverlapParams - New Spatial Query API - #107 by tnavarts
Disregard the crossed out text

For the OP, just use the more recent functions that @C_Sharper provided, as it can easily detect whenever a Part is touched regardless of it’s properties (Which is a better improvement over GetTouchingParts()), or you could also use GetPartBoundsInBox/GetPartBoundsInRadius which can be used to obtain near parts within a Square/Circle range

1 Like