Dictionary Inventory for every player

Hello, I’m looking to make a dictionary for every player. It will hold certain data that will save such as knives/effects and more soon.

How it will look:
`data.Inventory = {

['PlayerUsernameHere']={
	['Knives'] = {'test','test2'};
	['Effects'] = {};
}

}`

How do I make it so that every player can have their own part of the dictionary? For example when there is a playeradded event fired, it will copy and paste that table for the player that joined and use that to getasync/setasync on certain data.

There is a great module developed by loleris called ProfileService that I recommend checking out. It makes doing what you described really easy and reliable. I personally used it for the saving system in my theme park game. The module allows you to load a “profile” for each player which contains all of their save data in a format you define with a dictionary template. The example code is a great way to get started.

Hope this helps :slight_smile:

Would most likely look like this

local Players = game:GetService("Players")
local DataTemplate = {
	['Knives'] = {};
	['Effects'] = {};
}

local SessionData = {}

Players.PlayerAdded:Connect(function(player)
   local data --probably use getasync to fetch combined knife and effects data here
   local newData = DataTemplate --get a new template and write over the values if it exists, else use the defaults
   newData.Knifes = data.Knifes or DataTemplate.Knifes
   newData.Effects = data.Effects or DataTemplate.Effects
   SessionData[player] = newData --save the player's data to a global dictionary for all parts of the script to use
end)

I agree that ProfileService is great for this, assuming you need to save the data.

If you want a quick an dirty way to achieve the same thing try this:

  1. Create a ModuleScript saved in ServerScriptService, called “DataModule”
-- Data Module
local Data = {}
local PlayerData = {}

function Data.AddPlayer(Player, Data) -- adds a player to the PlayerData Table (Do this when a player joins)
	PlayerData[Player] = Data
end

function Data.ReturnData(Player) -- Retrieves a player's data from the PlayerData Table 
	return PlayerData[Player]
end

function Data.RemovePlayer(Player) -- Removes a player from the PlayerData Table (Do this when a player leaves)
	PlayerData[Player] = nil
end

return Data
  1. Create a Script in ServerScriptService to initilaise the data
-- SERVICES
local Players = game:GetService("Players")

-- MODULES
local DataModule = require(game.ServerScriptService.DataModule)

-- VARIABLES
local Data = { -- Example Data
	team = 0,
	cash = 10,
	xp = 10,
	tool1= true,
	tool2= false,
	tool3= false,
	equipped = "tool1"
}

-- FUNCTIONS
-- Creates Player Data on Join
local function onPlayerJoin(player)
	DataModule.AddPlayer(player, Data)
end

-- Removes Player Data on Leave
local function removeActivePlayer(player)
	DataModule.RemovePlayer(player)
end

-- EVENTS
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(removeActivePlayer)

Now other scripts can access the Data Module as well, just require the module and you can add/remove/update that data as you want:

  1. Any other script as long as it can “see” the DataModule. Place it into ReplicatedStorage if client side scripts need to use the data:
-- MODULES
local DataModule = require(game.ServerScriptService.DataModule)

-- Rear Data
local playerData = DataModule.ReturnData(player)

-- Update Data
playerData["cash "] = playerData["cash "] + 10
1 Like