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
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
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”
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
“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)
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().