Storing player data in game


#1

So currently I do what I feel is a extremely outdated, I use values to store all player information as oppose to something else holding it all. So when I want to access for an example a equipped weapon I go to player>Data>EquippedTool>Value. I feel there must be some way to do this better potentially using modules? Also saving/loading games is kind of irritating to, this way works just wondering if it can be done better.

How my current system works is
image
And then gets placed into the player:
image

I’m wanting to figure out a way to do this before starting on saving data as currently looping through inventory items and saving different stats of each item just seems to be terribly on my part.


#2

It is very outdated, for data if you ever plan on using values it should be for leaderstats only. Use a ModuleScript for replication.


#3

Yes modules are what you want to use

ModuleScript

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

Script

local DataModule = require(game.ServerScriptService.DataModule)
local Players = game:GetService("Players")

local Replicated_Storage = game:GetService("ReplicatedStorage")
local Event = Replicated_Storage:WaitForChild("RemoteEvent")


local Data = {Level = 1, Money = 2}

function PlayerJoined(Player)
	DataModule.AddPlayer(Player, Data) -- Data would be the data retrieved using datastores
end

function PlayerLeft(Player)
	DataModule.RemovePlayer(Player) -- have to remove the player when they leave so their data doesn't stay in game
end
function Example(Player)
	--you can retrieve a players data and do stuff with it by doing this
    local Data = DataModule.ReturnData(Player) -- now you have the table of their data to do with as you please
end

function RetrieveData(Player)
	Event:FireClient(Player, DataModule.ReturnData(Player)) -- when a player fires the remoteevent asking for data we fire it back returning their data
end

Players.PlayerAdded:Connect(PlayerJoined)
Players.PlayerRemoving:Connect(PlayerLeft)

Event.OnServerEvent:Connect(RetrieveData)

Local

local Player = game.Players.LocalPlayer

local Replicated_Storage = game:GetService("ReplicatedStorage")
local Event = Replicated_Storage:WaitForChild("RemoteEvent")

local MyData -- creates MyData variable

function SetupPlayer()
Event:FireServer() -- tells the server that you are requesting your data

wait(MyData) -- waits for the data to arrive

for i, v in pairs(MyData) do -- just a simple loop to print all of your data
	print(i, v)
end

end

function UpdateData(NewData)
	MyData = NewData -- sets the MyData table to the new data you have recieved
end

Event.OnClientEvent:Connect(UpdateData)

SetupPlayer()

image
prints data

if you have any questions at all just reply I’m here to help