Linking a dictionary with a player

Okay so you know you can create a folder as a child of a player then put a value within it right?
i want to do the same thing, but with a dictionary. How do i do that.

(for more information, i have a inventory and i want to save it.)

2 Likes

oh also, i have to access the dictionary from multiple scripts, that is why i asked the question

Assuming you said dictionary, I think you can use a table with some attributes from your scripts.

1 Like

acutally what is table with attributes

1 Like

Assuming the dictionary isn’t huge(above 200k characters when encoded as a string) you can use JSONEncode and store it as an attribute within that player instance:

local dictionary = {
	level = 5,
	inventory = {},
	tools = {}
}

local Player = --reference to player
Player:SetAttribute("Dictionary", game.HttpService:JSONEncode(dictionary))

--to retrieve it from another script:
local dictionary = game.HttpService:JSONDecode(Player:GetAttribute("Dictionary"))

you don’t need to enable HttpService for this.

If you try to store large dictionaries you can try compressing the JSONEncoded string using libraries such as this.

thanks, I can put it in a local script right?

If you put it in a local script it won’t replicate to the server, so server scripts can’t see it.

But if you only plan to use this for the client(aka not datastore-related things) then it shouldn’t cause any issues.

so i got a question.

let say i got a script here

local dictionary = game.HttpService:JSONDecode(Player:GetAttribute("Dictionary"))

dictionary = {a = 10,
                    b = 5}

will the attribute of the player change when i do that?

You can make a module that returns a dictionary with player dictionaries with the players as keys.

Nope, you will have to use the first method to save it(using JSONEncode), here’re the 2 important functions if you want to save space:

type val = (string | number | boolean)?
type dict = val | {[number | string]: val}

local function getDictionary(plr: Player): dict
	local encoded = plr:GetAttribute("Dictionary")
	if encoded == nil then return nil end
	return game.HttpService:JSONDecode(encoded)
end

local function setDictionary(plr: Player, dict: dict)
	plr:SetAttribute("Dictionary", game.HttpService:JSONEncode(dict))
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.