What is a way to loop-check without lag?

while true do
	wait(.1)
	if #game.Workspace.MapStorage:GetChildren() == 1 then
		script.Parent.Region.Script.Disabled = true
	
	elseif #game.Workspace.MapStorage:GetChildren() == 0 then
		script.Parent.Region.Script.Disabled = false
	end
end

I haven’t used studio in a long time, but as the title says, what is another way to “loop-check” this?
This gives my game lag because it checks every 0.1 second if something happens.

0.1 normally gives alot of lag, try like 0.3 or 0.5

2 Likes

Instead of looping it every 0.1 seconds just to check for the changes you could use ChildAdded and ChildRemoved instead.

game.Workspace.MapStorage.ChildAdded:Connect(function()
    if #game.Workspace.MapStorage:GetChildren() == 1 then
	    script.Parent.Region.Script.Disabled = true
    elseif #game.Workspace.MapStorage:GetChildren() == 0 then
        script.Parent.Region.Script.Disabled = false
    end
end)

game.Workspace.MapStorage.ChildRemoved:Connect(function()
    if #game.Workspace.MapStorage:GetChildren() == 1 then
	    script.Parent.Region.Script.Disabled = true
    elseif #game.Workspace.MapStorage:GetChildren() == 0 then
        script.Parent.Region.Script.Disabled = false
    end
end)
2 Likes

wait() is defaulted to 0.3, no matter how close to zero you set it.

1 Like

That is not the case. The minimum is every other frame (typically 1/30th of a second, or .033 seconds).

image

5 Likes

A more compact solution:

local mapStorage = game.Workspace.MapStorage

local function checkChildren()
	local numChildren = #mapStorage:GetChildren()
	if (numChildren <= 1) then
		script.Parent.Region.Script.Disabled = (numChildren == 1)
	end
end

mapStorage.ChildAdded:Connect(checkChildren)
mapStorage.ChildRemoved:Connect(checkChildren)
checkChildren() -- May or may not be required
2 Likes