There are three scripts that load process and save the data
I’m not sure how to make the script appear right within the message so i appologise for that.
The first script loads data
local playerDataStore = require(script.Parent.PlayerDataStore)
local function onPlayerJoin(player)
local loadedData = playerDataStore.getDataForPlayer(player)
local leaderstats = player:FindFirstChild(“leaderstats”)
local Players = game:GetService(“Players”)
Players.PlayerAdded:Connect(function(Player)
-- make the leaderboard
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local exp = Instance.new("IntValue")
local exp = player.leaderstats:FindFirstChild("Experience")
exp.Value = 0
exp.Name = "Experience"
exp.Value = loadedData.exp
exp.Parent = leaderstats
local Bucks = Instance.new("IntValue")
local Bucks = player.leaderstats:FindFirstChild("Bucks")
Bucks.Value = 0
Bucks.Name = "Bucks"
Bucks.Value = loadedData.Bucks
Bucks.Parent = leaderstats
end)
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
The second saves the data
– load in the PlayerDataStore module
local playerDataStore = require(script.Parent.PlayerDataStore)
local function onPlayerJoin(player)
– get the player’s information from the data store,
– and use it to initialize the leaderstats
local loadedData = playerDataStore.getDataForPlayer(player)
-- make the leaderboard
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local exp = Instance.new("IntValue")
exp.Name = "Experience"
exp.Value = loadedData.exp
exp.Parent = leaderstats
local Bucks = Instance.new("IntValue")
Bucks.Name = "Bucks"
Bucks.Value = loadedData.Bucks
Bucks.Parent = leaderstats
print(“Player data loaded”)
end
local function onPlayerExit(player)
– when a player leaves, save their data
local playerStats = player:FindFirstChild(“leaderstats”)
local saveData = {
exp = playerStats.Experience.Value,
Bucks = playerStats.Bucks.Value,
}
playerDataStore.saveDataForPlayer(player, saveData)
print(“Player Data Saved”)
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
The third proccess the data within a module
– Make a database called PlayerExperience, we will store all of our data here
local DataStoreService = game:GetService(“DataStoreService”)
local playerStore = DataStoreService:GetDataStore(“PlayerExperience”)
local defaultData = {
exp = 150,
rank = 0,
–coins = 0,
–rebirths = 0,
}
local PlayerDataStore = {}
function PlayerDataStore.getDataForPlayer(player)
– attempt to get the data for a player
local playerData
local success, err = pcall(function()
playerData = playerStore:GetAsync(player.UserId)
end)
-- if it fails, there are two possibilities:
-- a) the player has never played before
-- b) the network request failed for some reason
-- either way, give them the default data
if not success or not playerData then
print("Failed to fetch data for ", player.Name, " with error ", err)
playerData = defaultData
else
print("Found data : ", playerData)
end
-- give the data back to the caller
return playerData
end
function PlayerDataStore.saveDataForPlayer(player, saveData)
– since this call is asyncronous, it’s possible that it could fail, so pcall it
local success, err = pcall(function()
– use the player’s UserId as the key to store data
playerStore:SetAsync(player.UserId, saveData)
end)
if not success then
print(“Something went wrong, losing player data…”)
print(err)
end
end
return PlayerDataStore