Infinite yield while object is already loaded

Hey everyone! I’m trying to create a first person item carrying system, but for some reason my script is throwing a Infinite yield or a Does Not Exist if I remove the :WaitForChild()

Here’s the script:

local pickables = game.Workspace:GetChildren("Pickable")


for _, pickables in ipairs(pickables) do
	local weight = pickables:WaitForChild("Weight")
	local destiny = pickables:WaitForChild("Destiny")

	pickables.MouseClick:Connect(function()
		print(weight.Value)
	end)
end

And here’s how my model is parented:
image

script is running in the StarterPlayerScripts

1 Like

For one, GetChildren doesn’t take any input arguments. You’re also overwriting the variable “pickables” within the for loop.

If the “Pickable” are anywhere within the workspace, you’ll have to do something like this:

for _,pickable in workspace:GetDescendants() do
	if pickable.Name == 'Pickable' then
		-- do stuff
	end
end

However, it should be noted that not everything will have loaded immediately by the time your script runs. In that case, you can combine that with:

workspace.DescendantAdded:Connect(function(pickable)
	if pickable.Name == 'Pickable' then
		-- do stuff
	end
end)

It just depends on where things are and what you’re doing.

Also the reason GetChildren and WaitForChild didn’t work is because “Pickable”, at least in the screenshot, is not a child of the workspace. A child is a direct descendant, where as a descendant is any descendant, no matter how deep.

2 Likes

you did game.Workspace:GetChildren("Pickable") which as 2 problems.

  1. Pickable isn’t a child of the workspace, it’s a child of BigBox.
  2. i do not believe can do :GetChildren("instance name here"), :GetChildren() returns an array of all children within the instance you called the method from.

you’d have to do something like this:

local pickables = game.Workspace.BigBox:GetChildren()


for _, pickables in ipairs(pickables) do
	local weight = pickables:WaitForChild("Weight")
	local destiny = pickables:WaitForChild("Destiny")

	pickables.MouseClick:Connect(function()
		print(weight.Value)
	end)
end
2 Likes

actually, adding onto this there’re a few more issues with the for loop.

you have pickables as the array you’re iterating through but at the same time defining pickables as the value variable which i don’t think is necessarily an issue but will throw you off rereading through this in a future.

also i do not believe that MouseClick is a BindableEvent of an Instance, you’d have to put a ClickDetector in it and do pickables.ClickDetector.MouseClick

1 Like

“Pickable” is a ClickDetector, also you’ve kind of repeated everything I’ve already written.

i’m still not used to these new icons :sob:

Yeah, it is. Also the Infinite Yield is gone, but I’m struggling with how to make the Click function. Should I assign it to “pickable” or I need to do something else?

You had it right before with the ClickDetector.

Pickable.MouseClick:Connect(function()
	-- do stuff
end)

You don’t want to use the original “pickable” variable.

Here’s an example if using both the for loop iteration and event:

function SetupPickable(pickable)
	if pickable:IsA('ClickDetector') and pickable.Name == 'Pickable' then
		pickable.MouseClick:Connect(function()
			-- do stuff
		end)
	end
end

for _,pickable in workspace:GetDescendants() do
	SetupPickable(pickable)
end

workspace.DescendantAdded:Connect(function(pickable)
	SetupPickable(pickable)
end)

However, keep in mind that this can cause a memory leak if you’re removing the Clickable thing, so I would actually go as far as to do this:

function SetupPickable(pickable)
	if pickable:IsA('ClickDetector') and pickable.Name == 'Pickable' then
		local click = pickable.MouseClick:Connect(function()
			-- do stuff
		end)
		pickable.Destroying:Once(function()
			click:Disconnect()
			pickable = nil
			click = nil
		end)
	end
end

for _,pickable in workspace:GetDescendants() do
	SetupPickable(pickable)
end

workspace.DescendantAdded:Connect(function(pickable)
	SetupPickable(pickable)
end)

That way you can have an if statement within the -- do stuff part, and if it goes through and removes the ClickDetector (and any relevant parent(s)), it will clean up automatically.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.