InInfinite yield possible

Hi, my script is getting an error: Infinite yield possible on ‘Players.XceptDev.Trails: WaitForChild (“BlueTrail.Value”)’
and i have no idea how to fix it. I am counting on help.
script:

local player = game.Players.LocalPlayer
local trails = player:WaitForChild("Trails")

repeat
	wait(4)
	if trails:WaitForChild("BlueTrail").Value == 1 then
		script.Parent.Text = "Equip"
	else
		script.Parent.Text = "Unowned"
	end
	wait()
until game.Workspace.Storage.Lobby.Spawns.SpawnLocation.Anchored == false

You should not be using WaitForChild for checking if an object exists, you should be using it when you know it exists and waiting for it to load. FindFirstChild is probably more suitable in this case

1 Like

like that?

local player = game.Players.LocalPlayer
local trails = player:FindFirstChild("Trails")

repeat
	wait(4)
	if trails:FindFirstChild("BlueTrail").Value == 1 then
		script.Parent.Text = "Equip"
	else
		script.Parent.Text = "Unowned"
	end
	wait()
until game.Workspace.Storage.Lobby.Spawns.SpawnLocation.Anchored == false
2 Likes

try removing the waitforchild’s in the repeat loop. if it still does not work than remove all the waitforchild’s.

arter that i have that errors:
Infinite yield possible on ‘Players.XceptDev.Trails:WaitForChild(“BlueTrail.Value”)’
and
Trails is not a valid member of Player “Players.XceptDev”

1 Like

it seems that they did not find it. When they don’t find the object it gives infinite yield. try looking again to see if it is really there

Screenshot_5
is

2 Likes

I got something like this several time’s I forgot how I fixed it.

You don’t have to use WaitForChild in the line:

if trails:WaitForChild("BlueTrail").Value == 1 then

you can just do:

if trails.BlueTrail.Value == 1 then

It usually works fine, so the infinite yeld possible error won’t show up.

2 Likes

exactly what I was trying to explain. waitforchild is not necessary but apparently it still does not work for him.

1 Like

i did it and it shows this error:
Infinite yield possible on 'Players.XceptDev.Trails:WaitForChild(“BlueTrail.Value”)

Value is not a child, but a property

If it’s a string value, then that line of code will work fine.

WaitForChild() should be used when you want to wait for an object to exist. This causes your script to hang. Since it’s waiting for the object to exist before continuing. You can actually set a timeout variable in WaitForChild(), which will set a limit to how long it waits. For example,

local trails = player:WaitForChild("Trails", 5)

The above code will cause your script to check for “Trails” to exist in a 5 second timespan before continuing.

FindFirstChild() should be used when you want to check if an object exists or not. Whatever the result is, the script will continue execution without waiting for that object. The method returns nil if the object is not found.

Whichever method you use, you should add a nil check in the repeat loop. If the object doesn’t exists while evaluating your if statement conditions, you’ll always receive an error. You’d essentially be doing the following:

If trails did not exist:
if nil.BlueTrails.Value == 1 then

if BlueTrails did not exist:
if trails.nil.Value == 1 then

Add the following to your code to check if an object exists:

repeat
	wait(4)
    if trails ~= nil then -- Check if trails exists.
        local bluetrail = trails:FindFirstChild("BlueTrail") -- Check if bluetrail exists.
	    if bluetrails ~= nil and bluetrail.Value == 1 then
		    script.Parent.Text = "Equip"
	    else
		    script.Parent.Text = "Unowned"
	    end
    end
    wait()
until game.Workspace.Storage.Lobby.Spawns.SpawnLocation.Anchored == false
1 Like

“infinite yield possible” is a warning (note: warning, not error) that the script can yield (wait/stop) forever because you haven’t set a time out for it. It appears after a short time if there is no time out length set for it, since it’s kinda bad practice to not have a time out set (if the thing you’re waiting for never turns up, the script will never continue)

1 Like

It’s not a bad practice if you don’t set a timeout. It can actually be very useful. For example, if I made a redundant script to kick you out every time you joined, I could do the following:

while true do
    game.Players:WaitForChild("cpguy5089"):Kick()
end

Every time you join, you’ll get kicked and it will wait for you again. Everything has a proper use case.

Bad practices would be to check if an object exists by using WaitForChild(object, 0.1) instead of using FindFirstChild().