Infinite yield possible on 'Players:WaitForChild("Instance")'

So I have some code here

for i,v in pairs(script.Parent.Essentials:GetChildren()) do
		local Owner = script.Parent.Owner
		if v.Name == "PartCollector" then
			v.Touched:connect(function(Part)
				if Part:FindFirstChild('Cash') then
					if Owner.Value ~= nil then
						local player = game.Players:WaitForChild(Owner.Value)
						local gamepasses = player:WaitForChild("Gamepasses")
						
						if gamepasses["Auto Collect Cash"].Value == true then
							local playerMoney = game.ServerStorage.PlayerMoney:FindFirstChild(player.Name)
							if playerMoney ~= nil then
								playerMoney.Value = playerMoney.Value + (Part.Cash.Value * gamepasses.Cash.Value)
							end
						elseif gamepasses["Auto Collect Cash"].Value == false then
							Money.Value = Money.Value + (Part.Cash.Value * gamepasses.Cash.Value)
							Debris:AddItem(Part,0.1)
						end
					end
				end
			end)
		end
	end

And I get this in the output:

22:56:03.939 - Infinite yield possible on ‘Players:WaitForChild(“Instance”)’
22:56:03.940 - Stack Begin
22:56:03.941 - Script ‘Workspace.Zednov’s Tycoon Kit.Tycoons.Brownie.PurchaseHandlerNew’, Line 36
22:56:03.942 - Stack End

Line 36 is here:

local player = game.Players:WaitForChild(Owner.Value)

Why is this happening? Also, here is a picture of the explorer/properties
Screen Shot 2020-03-29 at 11.00.54 PM


EDIT: I kinda solved the problem. Read this post:

4 Likes

What are you setting the ObjectValue’s Value to?

It looks like you are waiting for an instance when you need to define it as either a string or a number.

If Owner is the player object you can simply do this to get that player:

local player = Owner.Value
2 Likes

try

local player = game.Players:WaitForChild(Owner.Value.Name)
1 Like

You’re putting Owner.Value as one of the parameters, move .Value outside.

Edit: I’m an idiot, don’t worry about what I said.

3 Likes

It is merely a warning that it may not ever find Owner.Value. Warnings appear orange and they do not indicate that your script will not run. An error appears red and tells you that your code will not run or ended abruptly.

If Owner.Value is a player, you can just refer to it directly:
local player = Owner.Value

Otherwise, if Owner.Value is a string, then you are fine so long as Owner.Value is set correctly prior to using WaitForChild. If Owner.Value is an Instance, you need to use Owner.Value.Name. If Owner.Value is going to be updated while waiting, you can run this instead.
while not game.Players:WaitForChild(Owner.Value) do wait(1) end
Note that the warning will probably not go away, these are just some things to double-check. Lua is not very powerful as far as detecting things that will go wrong later, and it really shines here.

4 Likes

This is a common error. To solve this just put a comma and a number next to the “Instance”. E.g.

Players:WaitForChild("Instance",5)

The number just means the time in seconds the system will keep checking for that child. When the time ends the system will stop checking. Infinite yield possible just means that the system will keep checking until it finds the child and can go on forever.

6 Likes

I tried local player = Owner.Value and the warning did not come up again. Now the only problem is that when the collecter collects the drops, the money to collect does not go up…

You can’t wait on an instance value, it should be like this:

local player = game.Players:WaitForChild("Owner").Value

To sum up, first you need the quotations around the name of the variable. The other thing is that Owner.Value is not an instance. The .Value part is a property of it, thus it won’t exist if you look for it in the workspace.

Edit:
Building on what the others above have said, you don’t need to worry about waiting for “Owner” to exist. Since you are running it off of an event that is likely to take place after everything is loaded in the game, it’s safe to use local player = Owner.Value instead. Calling WaitForChild will just add more latency in the script.

I used

local player = Owner.Value

but then with the use of Breakpoints the game stopped while testing in studio. I guess the game could not find Owner.Value…
Screen Shot 2020-03-30 at 7.55.01 PM

1 Like

Breakpoints should never just “stop.” Is there not another error?

I just found a solution so yeah.

It may be a silly solution, but it is effective:
define your variable as a string as follows

local owner = script.Parent.Owner.Value ..""
local player = game.Players:WaitForChild(owner)
2 Likes