Script Timeout Error?

My script keeps timing out. It ONLY happens if there is NO player inside of Region3.

Script timeout: exhausted allowed execution time

local WaitTimePod1 = game.ReplicatedStorage.WaitTimePod1
local RegionPod1 = workspace:WaitForChild("RegionPod1")

local TeleportService = game:GetService("TeleportService")

local Found = false

game.Players.PlayerAdded:Connect(function(player)
	while true do
		
		if WaitTimePod1.Value > 0 then
			wait(1)
			WaitTimePod1.Value = WaitTimePod1.Value - 1
			print(WaitTimePod1.Value)
			if WaitTimePod1.Value == 0 then
				local region = Region3.new(RegionPod1.Position - (RegionPod1.Size/2), RegionPod1.Position + (RegionPod1.Size/2))
				local parts = game.Workspace:FindPartsInRegion3WithWhiteList(region, player.Character:GetDescendants())
				print('IT TIMES OUT RIGHT AFTER THIS PRINT IS RAN')
				for _, part in pairs (parts) do
					if part:FindFirstAncestor(player.Name) then
						Found = true
						break
					else
						WaitTimePod1.Value = 30
						Found = false
					end
				end
			
				if Found == true then
					WaitTimePod1.Value = 30
					print('THE TIMEOUT ERROR BRINGS ME TO THIS LINE BELOW')
					TeleportService:Teleport(5088845478, player)
				end
			end
		end
	end
end)

I believe it’s because while the WaitTimePod1.Value < 0 the loop has nothing calling it to wait()

1 Like

replace while true do with while wait() do

4 Likes

Please do NOT use while wait.

Put the wait in the loop body itself at the end of the iteration (before the last wait, or wherever appropriate). You should never put wait there. Use the condition properly.

2 Likes

Do not use While wait() do to stop Timeout errors, yes, you can use it, and if you want to test your stuff, but don’t try to use it in a published game, in this case, you could use the .Changed event, if you want to use a Changed event with only one property, then use GetPropertyChangedSignal:

local WaitTimePod1 = game.ReplicatedStorage.WaitTimePod1
local RegionPod1 = workspace:WaitForChild("RegionPod1")

local TeleportService = game:GetService("TeleportService")

local Found = false

game.Players.PlayerAdded:Connect(function(player)
	WaitTimePod1:GetPropertyChangedSignal("Value"):Connect(function() --//Changed line
		if WaitTimePod1.Value > 0 then
			wait(1)
			WaitTimePod1.Value = WaitTimePod1.Value - 1
			print(WaitTimePod1.Value)
			if WaitTimePod1.Value == 0 then
				local region = Region3.new(RegionPod1.Position - (RegionPod1.Size/2), RegionPod1.Position + (RegionPod1.Size/2))
				local parts = game.Workspace:FindPartsInRegion3WithWhiteList(region, player.Character:GetDescendants())
				print('IT TIMES OUT RIGHT AFTER THIS PRINT IS RAN')
				for _, part in pairs (parts) do
					if part:FindFirstAncestor(player.Name) then
						Found = true
						break
					else
						WaitTimePod1.Value = 30
						Found = false
					end
				end
			
				if Found == true then
					WaitTimePod1.Value = 30
					print('THE TIMEOUT ERROR BRINGS ME TO THIS LINE BELOW')
					TeleportService:Teleport(5088845478, player)
				end
			end
		end
	end)
end)

Hi, dockboy, please, don’t set this as a solution, my objective here is not to steal a solution. Thanks :slight_smile:

1 Like