#How to store player data

1 messages · Page 1 of 1 (latest)

brave ember
#

Hi, so I wanna how can I store player data in my game like coins,purchases,level and everything.

gaunt bone
#

search up rileybytes on youtube and youll find it

brave ember
gaunt bone
#

i don't think httpservice is used

brave ember
#

There is a option in studio

#

Enable http service

gaunt bone
#

oh you dont have to i think

glossy torrent
#

not profilestore

#

data store service

onyx perch
#

Ask @mint drum to memorize them for u

twilit merlin
#

I recommend always using pcall for it, to error treatment in case of failure

#
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local GameData = DataStoreService:GetDatatStore("GameData")

local DataContainer = {}

local function GetDefaultData()
  return {
    Money = 0,
    Level = 1,
    Exp = 0
  }
end

local function OnPlayerAdded(Player)
  local Success, Result = pcall(function()
    return GameData:GetAsync(Player.UserId)  
  end)

  if not Success then
    warn(`Error trying to load {Player.Name}'s data: {Result}`)
  end

  DataContainer[Player] = Result or GetDefaultData()
end

local function OnPlayerRemoving(Player)
  if not DataContianer[Player] then
    warn(`No data to save for {Player.Name}`)
    return 
  end
  local Success, Result = pcall(function()
    GameData:SetAsync(Player.UserId, DataContainer[Player])
  end)

  if not Success then
    warn(`Error trying to save {Player.Name}'s data: {Result}`)
  end
end

for _, Player in Players:GetPlayers() do OnPlayerAdded(Player) end
Players.PlayerAdded:Connect(OnPlayerAdded)
Players.PlayerRemoving:Connect(OnPlayerRemoving)
#

just made it now, but you can use as a reference: GetAsync to get player's data using an ID (usually player's user id), and to set (save) the same thing: using an ID, and passing the data you want to save

#

for the error treatment, I recomend using some retries (3 or more), and a delay for it

gaunt bone
onyx perch
#

ProfileStore is literally made with data store service

gaunt bone
#

yes i know but you suggested dss over profile store

gaunt bone
#

wait

#

wrong person

#

thats my bad