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
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)
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.
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.
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