Set an item name and item image if the player owns it

I am trying to get whatever item a player owns in the players folder in their folder called items, if the value of the item name in the folder is ‘owned’ then what happens is the text of the item shows up in one of the slots. And it also loads in the image, by checking the module.
Here is the code, but it isn’t working, so if I could get help. It would mean a lot.

local assets = require(game.ReplicatedStorage.ItemShop)
local player = game.Players.LocalPlayer
local r = player:WaitForChild("Items")
    for _, v in pairs(r) do
	if v.Value == 'owned' then
		local tt = assets:FindFirstChild(v.Name)
		script.Parent.Image = tt.Image
		print(tt.Image)
		script.Parent.name.Text = tt.Item
		print(tt.Item)
		
	end
end


Thanks!

1 Like

(Had to add a backslash due to formatting)

Probably this. If you checked your console, you could’ve figured it out. You need to call GetChildren on r, otherwise you attempt to iterate through a non-table value.

This as well. If v.Name isn’t a child of the ModuleScript, it’ll error. If the module returns a table, you need to access it via indice: assets[v.Name].

1 Like

How would get children work on this?

I tried this, but it isn’t working.

local assets = require(game.ReplicatedStorage.ItemShop)
local player = game.Players.LocalPlayer
local r = player:WaitForChild("Items")
local mr = r:GetChildren()
    for _, v in pairs(mr) do
	if mr.Value == 'owned' then
		local tt = assets[mr.Name]
		script.Parent.Image = tt.Image
		print(tt.Image)
		script.Parent.name.Text = tt.Item
		print(tt.Item)
			end
end


Colbert is correct. On line 6 of the above quote, .mr.Value should be v.Value. In this case, mr is the array of instances returned from :GetChildren() and v is the instance indexed in the array. Make sure to mark his reply as the solution once you have corrected the above snippet.

Visualised:

local mr = {Items.instance1, Items.instance2, Items.instance3}
for i, v in pairs(mr) do
    print(i) -- index, e.g 1, 2, 3.
    print(v.Value) -- v.Value, e.g true, false, "owned".
end

Mistakes such as these can often be remediated without seeking technical help if you look at the output window or developer console. Make sure you try to solve similar issues yourself in the future before seeking help, it helps you gain a much more thorough understanding of error handling and debugging :+1:.

Edit: make sure to check your codebase for similar issues, I noticed line 7 has the same issue as mentioned above.

Tried your method, it works as all I know. But as I showed, there is a new error. “tt” is apparently a nil value, all I do is check v.Name, and is shows as nil.

local player = game.Players.LocalPlayer
local r = player:WaitForChild("Items")
local shop = require(game.ReplicatedStorage.ItemShop)
wait(2)
local mr = {r['Dog'], r['Cat'], r['Candy Cane'], r['Rex Glider'],r['Scenario'],r['Plane Glider'],r['Dominus Rex'],r['Sparkle'],}
while wait(.6) do
for i, v in pairs(mr) do
	  print(i)
    print(v.Value) 
	if v.Value == 'owned' then
		local tt = shop[v.Name]
		print(tt)
		script.Parent.Image = tt.Image
		print(tt.Image)
		script.Parent.name.Text = tt.Item
		print(tt.Item)
			end
end
end


I didn’t mean to literally create an array of instances like that, I was simply visualising what :GetChildren() was abstracting. If you revert to your old codebase and apply the changes I mentioned and it still doesn’t function correctly, I’ll assist you from there.

local player = game.Players.LocalPlayer
local r = player:WaitForChild("Items")
local shop = require(game.ReplicatedStorage.ItemShop)
local mr = r:GetChildren()
while wait(.6) do
for i, v in pairs(mr) do
	  print(i)
    print(v.Value) 
	if v.Value == 'owned' then
		local tt = shop[v.Name]
		print(tt)
		script.Parent.Image = tt.Image
		print(tt.Image)
		script.Parent.name.Text = tt.Item
		print(tt.Item)
			end
end
end


New code^^ Still nil.

I assume you are trying to search the children of shop? Your shop variable is a required modulescript, not a reference to the physical modulescript itself. You can quickly change local tt = shop[v.Name] to local tt = game.ReplicatedStorage.ItemShop[v.Name] to check.

So what do I do? Unrequire it?

Your shop variable is whatever code the ModuleScript returned. If you want a physical reference, create a new variable and reference the ModuleScript object, e.g local PhysicalShop = game.ReplicatedStorage.ItemShop, then modify your tt variable to local tt = PhysicalShop[v.Name].

What do you mean by creating a physical reference?

A reference to the physical ModulceScript instead of the returned value from the ModuleScript, i.e:

local player = game.Players.LocalPlayer
local r = player:WaitForChild("Items")
local shop = require(game.ReplicatedStorage.ItemShop)
local physicalshop = game.ReplicatedStorage.ItemShop
local mr = r:GetChildren()
while wait(.6) do
for i, v in pairs(mr) do
	  print(i)
    print(v.Value) 
	if v.Value == 'owned' then
		local tt = physicalshop[v.Name]
		print(tt)
		script.Parent.Image = tt.Image
		print(tt.Image)
		script.Parent.name.Text = tt.Item
		print(tt.Item)
	end
    end
end

It looks like you are trying to index a child of the modulescript, which was nil because you require()ed it.

I used your script;

local player = game.Players.LocalPlayer
local r = player:WaitForChild("Items")
local shop = require(game.ReplicatedStorage.Item)
local physicalshop = game.ReplicatedStorage.Item
local mr = r:GetChildren()
while wait(.6) do
for i, v in pairs(mr) do
	  print(i)
    print(v.Value) 
	if v.Value == 'owned' then
		wait(2)
		local tt = physicalshop[v]
		print(tt)
		script.Parent.Image = tt.Image
		print(tt.Image)
		script.Parent.name.Text = tt.Item
		print(tt.Item)
	end
    end
end

Here is the module;

local Item = {
		['Dog'] = {
		['Image'] = 'rbxassetid://',
		['Rarity'] = 'Common',
		['Type'] = 'Outfit',
	};
		['Cat'] = {
		['Price'] = '2500',
		['Image'] = 'rbxassetid://',
		['Rarity'] = 'Common',
		['Type'] = 'Outfit',
	};
			['Candy Cane'] = {
		['Price'] = '500',
		['Image'] = 'rbxassetid://',
		['Rarity'] = 'Epic',
		['Type'] = 'Pickaxe',
	};
			['Scenario'] = {
		['Price'] = '250',
		['Image'] = 'rbxassetid://',
		['Rarity'] = 'Legendary',
		['Type'] = 'Emote',
	};
		['Rex Glider'] = {
		['Price'] = '1000',
		['Image'] = 'rbxassetid://',
		['Rarity'] = 'Rare',
		['Type'] = 'Glider',
	};
		['Plane Glider'] = {
		['Price'] = '2500',
		['Image'] = 'rbxassetid://',
		['Rarity'] = 'Uncommon',
		['Type'] = 'Glider',
	};
		['Dominus Rex'] = {
		['Price'] = '50000',
		['Image'] = 'rbxassetid://',
		['Rarity'] = 'Legendary',
		['Type'] = 'Hat',
	};
		['Sparkle'] = {
		['Price'] = '2055',
		['Image'] = 'rbxassetid://',
		['Rarity'] = 'Epic',
		['Type'] = 'Hat',
	};
		}

return Item

And I am getting an error that dog is not a valid member of ModuleScript.

Blockquote
local shop = require(game.ReplicatedStorage.Item)
local physicalshop = game.ReplicatedStorage.Item

Try making both folders a different name.

They are modules… not folders?

Both of them are the same module…?

I asked whether you were trying to index an actual child instance of the ModuleScript. Since you are not, then revert back from the physical reference.

local tt = shop[v.Name] will output the nil value error if there is not an entry inside the ModuleScript which is labelled as whatever v.Name is. If I change v.Name to cat, then it works as expected.

image

Make sure the values you are iterating through are actually entered in the module.