Attempting to use dictionaries after long neglect

I’ve never gotten the hang of dictionaries, I could only really do tables. I’m attempting to create a dictionary that will store the player object and then a table of items they were wearing (save outfit feature)

This is what I’ve currently got. I’m using the command bar to test this and I’m getting the error “Items is not a valid member of Player” I’m not attempting to get Items from the player itself but making one inside the dictionary.

My code:

local currentWearing = {}
local module = {}

module.CurrentWearing = function(Player, type, thing)
	if type == 'Add' then
		if not currentWearing[Player] then
			currentWearing[Player] = Player
		end
		
		if not currentWearing[Player]["Items"] then -- error here
			currentWearing[Player]["Items"] = {}
		end
		table.insert(currentWearing[Player]["Items"], thing)
		
	elseif type =='Remove' then
		currentWearing[Player]["Items"] = nil -- ignore this, not done yet
	end
end

local model = Instance.new('Model', workspace)
module.CurrentWearing(game.Players.SovereignFrost, 'Add', model)

What I’m attempting to do

local currentWearing = {
SovereignFrost = {
Items = {'Model'}
}
}

Any help will be gladly appreciated!

tl;dr: i suck with dictionaries

Target


Looks like a small error. Change Player to {}.

1 Like

Thank you, this stuff honestly stresses me out.

The way I think about dictionaries is just specifying a key-value pair in a table, whereas an array is only a value with a hidden implicit numerical key.

local This = {
    ["isA"] = "Dictionary!"
}

local This = {"isAn", "Array!"}

Though the terms matter, I’ve never quite bothered to the difference in terminology, let alone the difference in technical details. I just know that each exists and how they work.

2 Likes

Also just FYI, type is a Lua function, so using it as a variable is shadowing the function. It will still work, but might be best to use a different name

3 Likes