Can't locate folder Error

Hi! So I made an inventory and shop system but I am having a small error with this script.

function updateInventory()

	for i, child in pairs(invFrame.ShopGUI.Tools:GetChildren()) do
		if child:IsA("ImageLabel") then child:Destroy() end
	end


	local ownedTrails = ownedTrailsFolder:GetChildren()
	print(ownedTrails)

	table.sort(ownedTrails, function(a, b)
		return trailsFolder[a.Name].Price.Value < trailsFolder[b.Name].Price.Value or trailsFolder[a.Name].Price.Value == trailsFolder[b.Name].Price.Value and a.Name < b.Name
	end)


	for i, tool in pairs(ownedTrails) do
		local item = script.InvItem:Clone()
		item.Image = tool.Folder.ImageLabel.Image
		item.SelectButton.Text = "Equip"
		item.Title.Text = tool.Name
		item.Visible = true



		item.Parent = invFrame.ShopGUI.Tools






		item.SelectButton.MouseButton1Click:Connect(function()
			local price = tool.Price.Value
			local coins = player.leaderstats.Coins.Value
			if item == SelectedTrail then
				item.SelectButton.Text = "Equip"

				SelectedTrail = nil
			else
				if player.OwnedTools:FindFirstChild(tool.Name) or (price <= coins) then
					item.SelectButton.Text = "Unequip" 
				else
					item.SelectButton.Text = "Unequip"
				end
				if SelectedTrail then
					SelectedTrail.SelectButton.Text = "Equip"
				end

				SelectedTrail = item
			end

			TrailSelectedRE:FireServer(tool)
		end)
	end




end		

The error is at:

item.Image = tool.Folder.ImageLabel.Image

It says that Folder is not a valid image of the Gravity Coil when it is.
Picture:
image

Error image:

How do I fix this?

try adding a WaitForChild() when defining it

item.Image = tool:WaitForChild("Folder").ImageLabel.Image
2 Likes

That fixed it. I underestimate WaitForChild sometimes, its very useful. Thank you!

1 Like

Hey, do you mind helping me with one more issue. So in the inventory frame, you click this unequip button and it triggers the event. Here is the code:

ToolSelectedRE.OnServerEvent:Connect(function(plr, tool)
	if not tool or typeof(tool) ~= "Instance" then return end -- we still want this here just in case

	local ownedTool = plr.OwnedTools:FindFirstChild(tool.Name)
	print("triggered")



	if not ownedTool then
		print(plr.Name," is buying the ",tool.Name," tool.")

		local price = tool.Price.Value
		local coins = plr.leaderstats.Coins

		if price <= coins.Value then
			coins.Value -= price

			local newTool = tool:Clone() do
				newTool.Parent = plr.OwnedTools



			end
		end
	elseif ownedTool then
		if plr.Backpack:FindFirstChild(tool.Name) == nil then
			print(plr.Name," equipped ",tool.Name," tool.")

			local new = tool:Clone()
			new.Parent = plr.Backpack
	
			
		end
	else if plr.Backpack:FindFirstChild(tool.Name) then
			
			plr.Backpack:FindFirstChild(tool.Name):Destroy()
		end
		


	end
end)

The print statement at the start of the event is working so that isn’t the issue. It means that its not going through the proper if statement or the Destroy() isn’t working. How can I fix this?

Try printing before the Destroy() to see if it gets there

change else if to elseif and remove that extra end

I tried what you said and it doesn’t print.

Try doing this:

	elseif ownedTool then
		if plr.Backpack:FindFirstChild(tool.Name) == nil then
			print(plr.Name," equipped ",tool.Name," tool.")
			
			local new = tool:Clone()
			new.Parent = plr.Backpack
			
		elseif plr.Backpack:FindFirstChild(tool.Name) then
			
			plr.Backpack:FindFirstChild(tool.Name):Destroy()
			
		end
	end
end)

Instead of this:

	elseif ownedTool then
		if plr.Backpack:FindFirstChild(tool.Name) == nil then
			print(plr.Name," equipped ",tool.Name," tool.")

			local new = tool:Clone()
			new.Parent = plr.Backpack
	
			
		end
	else if plr.Backpack:FindFirstChild(tool.Name) then
			
			plr.Backpack:FindFirstChild(tool.Name):Destroy()
		end
		


	end
end)

Thank you, this worked! Can you help me one more time?
So for the inventory system I made there are equip buttons under each item. It looks like this:

When you equip an item, the text changes to unequip.

The problem is, if you have two items equipped, only one text button will say unequip instead of both of them. I don’t know what to change in my code to fix this.
Here is my code:

function updateInventory()

	for i, child in pairs(invFrame.ShopGUI.Tools:GetChildren()) do
		if child:IsA("ImageLabel") then child:Destroy() end
	end


	local ownedTrails = ownedTrailsFolder:GetChildren()
	print(ownedTrails)

	table.sort(ownedTrails, function(a, b)
		return trailsFolder[a.Name].Price.Value < trailsFolder[b.Name].Price.Value or trailsFolder[a.Name].Price.Value == trailsFolder[b.Name].Price.Value and a.Name < b.Name
	end)


	for i, tool in pairs(ownedTrails) do
		local item = script.InvItem:Clone()
		item.Image = tool:WaitForChild("Folder").ImageLabel.Image
		item.SelectButton.Text = "Equip"
		item.Title.Text = tool.Name
		item.Visible = true



		item.Parent = invFrame.ShopGUI.Tools






		item.SelectButton.MouseButton1Click:Connect(function()
			local price = tool.Price.Value
			local coins = player.leaderstats.Coins.Value
			if item == SelectedTrail then
				item.SelectButton.Text = "Equip"

				SelectedTrail = nil
			else
				if player.OwnedTools:FindFirstChild(tool.Name) or (price <= coins) then
					item.SelectButton.Text = "Unequip" 
				else
					item.SelectButton.Text = "Unequip"
				end
				if SelectedTrail then
					SelectedTrail.SelectButton.Text = "Equip"
				end

				SelectedTrail = item
			end

			TrailSelectedRE:FireServer(tool)
		end)
	end




end		

The button text changes through this code here:

	item.SelectButton.MouseButton1Click:Connect(function()
			local price = tool.Price.Value
			local coins = player.leaderstats.Coins.Value
			if item == SelectedTrail then
				item.SelectButton.Text = "Equip"

				SelectedTrail = nil
			else
				if player.OwnedTools:FindFirstChild(tool.Name) or (price <= coins) then
					item.SelectButton.Text = "Unequip" 
				else
					item.SelectButton.Text = "Unequip"
				end
				if SelectedTrail then
					SelectedTrail.SelectButton.Text = "Equip"
				end

				SelectedTrail = item
			end

			TrailSelectedRE:FireServer(tool)
		end)
	end

How do I make it so if both items are equipped in the players backpack, both text buttons will say “Unequip”?