Retrieving player data

What is the best way to get player data?

First option
Create a local player storage in ReplicatedStorage or in StarterPlayerScripts and after, for example, successfully loading the player data, send the data there, and process it there and send a request to the server.

Second option
Every time you need to get data from the server, send the player RemoteFunction and get the player data.

I use the Packet library

The best option is using OnPlayerAdded Event from server script, then retrieve everything from there

Correct me if this is not what you’re looking for

For example:
Player data is successfully received. Next, the server sends a RemoteEvent to the player using Packet lib. The player receives the data and saves it in his local storage.

by local storage what do you mean?

LocalScript in StarterPlayerScripts which storage player data from the server

actually, you could just make a value in the player, and the client can easily access n update from time to time

So there is no need to send RemoteFunction to get player data?

Not at all, you can just call this in the localscript:

local x = game.Players.username.ValueName

then whenever u wanna use it, use x.Value

This saves bandwidth as you dont need to send events to the client multiple times everytime the data is updated

PlayerDataStoreService

local PlayerDataStoreService = {}

local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Utils = require(ReplicatedStorage.Modules.Utils)
local StorageDataManager = require(script.Parent)
local PlayerDataEvents = require(ReplicatedStorage.Events.Data.PlayerDataEvents)

local DataField = Utils.DataField
local DataStore = StorageDataManager.DataStore

local PLAYER_DATA_FIELDS = {
	[DataField.TOKENS] = {
		default = 0
	},
	[DataField.MUSIC] = {
		default = true
	},
	[DataField.SFX] = {
		default = true
	}
}

local playerData = {}

local function getPlayerKey(player)
	return "Player_"..player.UserId
end

local function getPlayerData(player)
	return playerData[player.UserId]
end

local function setPlayerData(player, data)
	playerData[player.UserId] = data
end

local function resetPlayerData(player)
	setPlayerData(player, nil)
end

local function loadPlayerData(player)	
	local key = getPlayerKey(player)
	local success, data = pcall(function()
		return DataStore:GetAsync(key) or {}
	end)
	
	if success then
		for fieldName, config in pairs(PLAYER_DATA_FIELDS) do
			if data[fieldName] == nil then
				data[fieldName] = config.default
			end
		end
	else
		warn(`[{player.UserId}] - Data failed to load for player!`)
	end

	setPlayerData(player, data)

	return success
end

function PlayerDataStoreService.updatePlayerData(player, data, shouldSave)
	setPlayerData(player, data)

	if not shouldSave then
		print(`[{player.UserId}] - Data was successfully updated, without save!`)
		return
	end

	local key = getPlayerKey(player)
	local success, err = pcall(function()
		DataStore:UpdateAsync(key, function()
			return data
		end)
	end)

	if success then
		print(`[{player.UserId}] - Data was successfully updated!`)
	else
		warn(err)
	end

end

function PlayerDataStoreService.savePlayerData(player)
	local key = getPlayerKey(player)
	local success, err = pcall(function()
		DataStore:UpdateAsync(key, function()
			return getPlayerData(player)
		end)
	end)

	if success then
		print(`[{player.UserId}] - Data was successfully saved!`)
	else
		warn(err)
	end

	resetPlayerData(player)
end

local function initPlayerData(player)
	PlayerDataEvents.onPlayerDataInitalized:FireClient(player, getPlayerData(player))
end

function PlayerDataStoreService.tryInitPlayerData(player)
	local success = loadPlayerData(player)
	if not success then
		return
	end

	initPlayerData(player)
end

return PlayerDataStoreService

PlayerDataHandler

local PlayerDataHandler = {}

local PlayerReceiveEvents = require(script.PlayerReceiveEvents)
local PlayerSendEvents = require(script.PlayerSendEvents)

local data = {}

function PlayerDataHandler.get(fieldName)
	return data[fieldName]
end

function PlayerDataHandler.getData()
	return data
end

function PlayerDataHandler.update(fieldName, value, shouldSave)
	data[fieldName] = value
	print(data)
	PlayerSendEvents.updateServerPlayerData(data, shouldSave)
end

function PlayerDataHandler.onPlayerDataInitialize(initData)
	data = initData
	print(data)
end

function PlayerDataHandler.init()
	PlayerReceiveEvents.init()
end

return PlayerDataHandler

PlayerDataHandler it’s storage local player data

Yes, you can can use a value in side the player, where the script loads the data to the value, then the client script will use data from the value in the player.

1 Like

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