0
\$\begingroup\$

I am a rookie at Lua and Roblox Studio in general. I am attempting an easy and reasonably simple tutorial on how to make a pretty good looking tycoon game. In my "Core" script, I am attempting to make it so when you press a button, a dropper appears, or whatever the StringValue has assigned it to (it is inside the button model). Here is the core script:

local tycoon = script.Parent.Parent

local mainItems = tycoon:FindFirstChild("MainItems")
local values = tycoon:FindFirstChild("Values")

local buttons = tycoon:FindFirstChild("Buttons")
local purchasedItems = tycoon:FindFirstChild("PurchasedItems")

local objects = {}

mainItems.OwnerDoor.Door.Touched:Connect(function(hit)
    if values.OwnerValue.Value == nil then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player then
            if player:FindFirstChild("HasTycoon").Value == false then
                values.OwnerValue.Value = player
                mainItems.OwnerDoor.Title.SurfaceGui.TextLabel.Text = tostring(values.OwnerValue.Value).."'s Tycoon"
            end 
        end
    end
end)


if buttons then
    for i, v in pairs(buttons:GetChildren()) do
        spawn(function()
            if v:FindFirstChild("Button") then

                local newObject = purchasedItems:FindFirstChild(v.Object.Value)
                if newObject ~= nil then
                    objects[newObject.Name] = newObject:Clone()
                    newObject:Destroy()
                else
                    v:Destroy()
                end

                if v:FindFirstChild("Dependency") then
                    v.Button.Transparency = 1
                    v.Button.CanCollide = false
                    v.Button.BillboardGui.Enabled = false
                    coroutine.resume(coroutine.create(function()
                        if purchasedItems:WaitForChild(v.Dependency.Value) then
                            v.Button.Transparency = 0
                            v.Button.CanCollide = true
                            v.Button.BillboardGui.Enabled = true
                        end
                    end))
                end

                v.Button.Touched:Connect(function(hit)
                    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
                    if player then
                        if values.OwnerValue.Value == player then
                            if v.Button.CanCollide == true then
                                if player:FindFirstChild("leaderstats").Cash.Value >= v.Price.Value then
                                    player.leaderstats.Cash.Value -= v.Price.Value
                                    objects[v.Object.Value].Parent = purchasedItems
                                    v:Destroy()
                                end
                            end
                        end
                    end
                end)
            end
        end)
    end
end

It is a pretty long script, but my error occurs on line 42. In my output, the error reads:

Infinite yield possible on 'Workspace.Tycoons.Tycoon.PurchasedItems:WaitForChild("Dropper")'. 
\$\endgroup\$
5
  • 1
    \$\begingroup\$ That message means that it looked for Dropper, but was unable to find it. I didn't see Dropper when I searched the Roblox Lua documentation & I didn't see it defined in the code you posted. Where are you intending it to find Dropper? Is it a script you made? \$\endgroup\$
    – Pikalek
    Commented Oct 21, 2023 at 19:14
  • \$\begingroup\$ I am not sure. It could possibly be the value of the Object Value inside of the button. As stated previously, I am following a tutorial, so here is the link, if it might help. The time where this script is found is at 5:15: youtube.com/… \$\endgroup\$ Commented Oct 21, 2023 at 19:37
  • \$\begingroup\$ At this point here in the video they explain how that line of code is supposed to work - it's dependent on the layout shown in the Explorer window. Specifically, inside the PurchasedItems folder there's a model named Dropper; does your Explorer window layout match? \$\endgroup\$
    – Pikalek
    Commented Oct 22, 2023 at 1:19
  • \$\begingroup\$ Yes, my Explorer window does match with the tutorial's. By the way, what does infinite yield actually mean? Online, I can't get a direct answer. \$\endgroup\$ Commented Oct 22, 2023 at 8:14
  • \$\begingroup\$ Sure thing. I've posted an answer that covers the meaning of the error. Based on the YouTube comments it looks like it's a known issue with the code. I took a shot at some potential work arounds & someone with more Lua knowledge might have something better to contribute. \$\endgroup\$
    – Pikalek
    Commented Oct 22, 2023 at 22:43

1 Answer 1

2
\$\begingroup\$

Let's look at line 42:

if purchasedItems:WaitForChild(v.Dependency.Value) then

And next let's look at the documentation for Instance:WaitForChild():

This function does not yield if a child with the given name exists when the call is made.

Instance:FindFirstChild() is a more efficient alternative to WaitForChild() for objects that are assumed to exist.

If a call to this method exceeds 5 seconds without returning, and no timeOut parameter has been specified, a warning will be printed to the output that the thread may yield indefinitely. This warning takes the following form where X is the parent's name and Y is the child's name:

Infinite yield possible on 'X:WaitForChild("Y")'

In other words, Infinite yield possible is warning you that when the code executed, the desired parameter didn't exist for more than 5 seconds. Often means you asked the code to find something that wasn't there.

In your case specifically, the parameter v.Dependency.Value resolved to "Dropper", but it was unable to get the Dropper object/model before it timed out.

In the comments, you said that your Explorer matched the one in the tutorial. That's good & probably covers the next part, but I'm going to mention this again anyway:

The first thing I would check it to make sure there's not some syntax error. Programming can be painfully literal doing what the code says rather than what was meant and a simple typo can lead to inscrutable errors. Double check the spelling and capitalization in your code, folder names and model names against the tutorial. Make sure that the folders are located in the appropriate locations. It may help to back track to this earlier tutorial video where the Dropper model is made.

That said, I did a quick skim of the YouTube comments noticed someone asked the author about the same error you had:

is it normal to get a infinite yield in dropper1?

Yes, that's because we are using WaitForChild("")

So you could have done everything exactly right (with respect to the tutorial) and still be having this problem. If that's the case, you consider some alternatives as described in this Stack Overflow answer. Here's a summary:

You could give the code a longer period before it times out by adding a second argument:

local timeout = 10 -- seconds
if purchasedItems:WaitForChild(v.Dependency.Value, timeout) then
    v.Button.Transparency = 0
    v.Button.CanCollide = true
    v.Button.BillboardGui.Enabled = true
else
    warn("Could not find v.Dependency.Value")    
end

You could also try changing from WaitForChild() to FindFirstChild(). The later won't generate Infinite yield possible errors, but it will give Attempt to index nil if it can't locate the target, so it might simply be trading one problem for another. And the tutorial author already uses FindFirstChild() in other parts of the code & is aware that WaitForChild() can result in Infinite yield possible errors. So I would assume that if swapping from WaitForChild() to FindFirstChild() was a fix, they would have mentioned that in the comments &/or updated the tutorial. I'm mentioning it as an option for the sake of completeness since it was discussed in the post I linked above.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you for this post. I think I have detected the problem, but I am unsure on a solution. As the dropper button should be there when the player starts the tycoon, there is no need for a dependency value. This means it can't find one when searching for it. But this leads to another problem, as if I put the dependency as blank, it will simply not appear. This post has really helped though. \$\endgroup\$ Commented Oct 24, 2023 at 17:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .