Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Infinite Yield possible on (clicked player)?

I am making a script where player1 clicks player2 then player2 has a gui pop up. Under StarterPlayerScripts I have a local script with:

--// Services
local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local Player = game.Players.LocalPlayer
--// Objects
local player = players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
	print("test1")
    local subject = players:GetPlayerFromCharacter(mouse.Target.Parent)
    if Player and Player.TeamColor.Name == ("Dirt brown") then
        replicatedStorage:WaitForChild("SellPistol"):FireServer(subject)
    end
end)

replicatedStorage:WaitForChild("SellPistol").OnClientEvent:Connect(function(subject)
    subject:WaitForChild("PlayerGui").StartScreen.Frame.Visible = true
print("test3")
end)

and under ServerScriptService I have:

--// Services
local replicatedStorage = game:GetService("ReplicatedStorage")

replicatedStorage:WaitForChild("SellPistol").OnServerEvent:Connect(function(player,subject)
	print("test2")
    game:GetService("ReplicatedStorage"):WaitForChild("SellPistol"):FireClient(player,subject)
end)

The result is:

Infinite yield possible on 'Players.example:WaitForChild("PlayerGui")'
--local script line 18

What does this mean?

1 answer

✓ Accepted answer

The warn message Infinite Yield occurs when a specific function like WaitForChild does a specific action, ie, waiting for a child named "potato" (or whatever the first argument is).

This warn message usually appears is so that the scripter will know what's going on instead of looking at their script and wondering why aren't some stuff are working.

WaitForChild is a function which yields ("pause") a script forever until a specific child with the name that matches with the first argument is parented under the instance. If you do not add a child with the name that matches the first argument, the function will yield the script indefinitely or for an infinite amount of seconds.

Using WaitForChild

local ham = Instance.new('Part',workspace);
ham:WaitForChild'Cheese';
print'Now I need bread'; -- < this does not print until an instance named "Cheese" is parented under the Instance we declared "ham"

1 comment

This discussion is preserved as it was. New answers, comments, and votes are closed.