ImageLabel:Clone() not working

I made a folder that has a bunch of image labels (used to be frames but i roundified them) and place that folder into replicatedstorage, now what I wanted to do was an Inventory System, so if the player has an item, the imagelabel with the exact name would be cloned from replicated storage and place into the scrollingFrame. So far, the only error is “Attempt to index nil with Clone()”

local templates = game.ReplicatedStorage:FindFirstChild("AllTemplates")
local content = script.Parent
local plr = game.Players.LocalPlayer
local boughtAxes = plr:WaitForChild("boughtAxes") or plr:FindFirstChild("boughtAxes")

while wait() do
	for i,v in pairs(boughtAxes:GetChildren()) do
		if v.Value == 1 then
			local pic = templates:FindFirstChild(v.Name)
			local clone = pic:Clone()
			clone.Parent = content
		elseif v.Value == 0 then
			print("Does Not Have This Item!!")
		end
	end
end
1 Like

Change

 local boughtAxes = plr:WaitForChild("boughtAxes") or plr:FindFirstChild("boughtAxes") 

to

local boughtAxes = plr:WaitForChild("boughtAxes")

You don’t need to do WaitForChild and FindFirstChild to make it work

That doesn’t help with the issue at hand.

I beleive that Attempt to index nil with Clone() will only occur when the object can’t be cloned. (Obviously).

Try printing things like pic.Name or pic.Parent and see if that prints successfully.

If the prints are unsuccessful, then we know that the script is not working because the image label doesn’t exist.

Possibly try to use

if v.Value == 1 and templates:FindFirstChild(v.Name) then

I’ve fixed up your code:

local templates = game.ReplicatedStorage:WaitForChild("AllTemplates")
local plr = game.Players.LocalPlayer
local boughtAxes = plr:WaitForChild("boughtAxes")

game:GetService("RunService").RenderStepped:Connect(function() -- the function will only run when the game renders a frame; so we reduce lag. consider running the function less.
  for i,v in pairs(boughtAxes:GetChildren()) do
		if v.Value == 1 and templates:FindFirstChild(v.Name) then
			local pic = templates[v.Name]
			local clone = pic:Clone()
			clone.Parent = script.Parent
		elseif v.Value == 0 then
			print("Does Not Have This Item!!")
		end
	end
end)
1 Like

You’re doing a lot of unnecessary :FindFirstChild() calls, this method should only be used for checking if a child exists or not (and in cases where you only have the object’s name). Are you sure that the axes exist beforehand inside of that folder?

@Winky_y Well it just means that pic is nil, and doing nil:Clone() would error.

1 Like

It most likely means pic is nil. Make sure to name the child of the boughtAxes folder the same as the imagelabel’s name in your templates folder.

Maybe do if pic ~= nil then

i have all the axes yet it spits out “Does Not Have This Item!!” about 200 times, yet there are only about 8 axes in the boughtAxes folder…

The reason it is printing “Does Not Have This Item!!” so much is because the script is looping…

If you want your script to run every few seconds add a number in between those brackets.

If you want your function to run every time your guy is opened, try turning what’s in the loop into a function and calling it on button.MouseButton1Down.

Try adding a print v.Value in there. If you are using the script I sent you and this prints “1”, then templates:FindFirstChild(v.Name) is nil.

Did you see my reply? 30 chars.

Your one point was already mentioned in previous posts.

I said to make sure the name of the picture was the same name of the child of the folder that holds the axe’s values.

It wasn’t just stating that it was nil.

Alright! Found the problem, but it seems to duplicate each axe I own, like a 100 times, i fixed that though. The problem was that the allTemplates folder had its imagelabels misnamed

Oh, ok.

Thats what I said. :man_facepalming: 30 chars.

ok… i didnt see your reply as I was focusing on Winky_y, then I saw your reply and checked, so yeah thanks