Simple enough, I was wondering how I would be able to add a datastore to my leaderboard, so once they join the game it loads their previous data, and if they left it saves the data. That’s all!
(simply, if they left with 5 smacks they rejoin with 5 smacks instead of 0)
Heres my current script:
local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local function leaderboardSetup(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Punches = Instance.new("IntValue")
Punches.Name = "Smacks"
Punches.Value = 0
Punches.Parent = leaderstats
local Ball = Instance.new("StringValue")
Ball.Name = "Ball"
Ball.Value = "Default"
Ball.Parent = leaderstats
end
Players.PlayerAdded:Connect(leaderboardSetup)
local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local LeaderboardDataStore = DataStore:GetDataStore("LeaderboardData") -- We get the datastore that being make one.
local function leaderboardSetup(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Punches = Instance.new("IntValue")
Punches.Name = "Smacks"
Punches.Value = 0
Punches.Parent = leaderstats
local Ball = Instance.new("StringValue")
Ball.Name = "Ball"
Ball.Value = "Default"
Ball.Parent = leaderstats
local success,result = pcall(function() -- This function is use to prevent errors in console and to see if error did occur.
return LeaderboardDataStore:GetAsync(player.UserId) -- We get our data.
end)
if success then -- We see if an error occurs or not.
if result then -- We see if player is new or not.
Punches.Value = result.Punches -- We set the data.
Ball.Value = result.Ball
else
print("Player is new.") -- Player is new.
end
else
warn("Error occured \n \n "..result) -- An error occured, log it.
end
end
local function SaveData(player)
local Data = {Ball = player.leaderstats.Ball.Value,Punches = player.leaderstats.Punches.Value} -- Our data table.
local success,result = pcall(function() -- Function to prevent and see errors in a safe way.
LeaderboardDataStore:SetAsync(player.UserId,Data) -- We save our data.
end)
if not success then
warn("Error occured \n \n "..result) -- Log our error.
end
end
Players.PlayerRemoving:Connect(SaveData)
Players.PlayerAdded:Connect(leaderboardSetup)
local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local LeaderboardDataStore = DataStore:GetDataStore("LeaderboardData") -- We get the datastore that being make one.
local function leaderboardSetup(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Punches = Instance.new("IntValue")
Punches.Name = "Smacks"
Punches.Value = 0
Punches.Parent = leaderstats
local Ball = Instance.new("StringValue")
Ball.Name = "Ball"
Ball.Value = "Default"
Ball.Parent = leaderstats
local success,result = pcall(function() -- This function is use to prevent errors in console and to see if error did occur.
return LeaderboardDataStore:GetAsync(player.UserId) -- We get our data.
end)
if success then -- We see if an error occurs or not.
if result then -- We see if player is new or not.
print("Loaded data!")
Punches.Value = result.Punches -- We set the data.
Ball.Value = result.Ball
else
print("Player is new.") -- Player is new.
end
else
warn("Error occured \n \n "..result) -- An error occured, log it.
end
end
local function SaveData(player)
local Data = {Ball = player.leaderstats.Ball.Value,Punches = player.leaderstats.Punches.Value} -- Our data table.
local success,result = pcall(function() -- Function to prevent and see errors in a safe way.
LeaderboardDataStore:SetAsync(player.UserId,Data) -- We save our data.
end)
if success then
print("Saved data!")
else
warn("Error occured \n \n "..result) -- Log our error.
end
end
Players.PlayerRemoving:Connect(SaveData)
Players.PlayerAdded:Connect(leaderboardSetup)
I added pruints to let you know that it did work. Also you need to update the values on the server not on the client.
It also didn’t work. Is there a specific problem happening? All it says is “Player is new”, and when I rejoin all the “smacks” reset back to 0. Edit: I’m going to try to use an alt to see if it was the button I was using causing problems
Edit again: Didn’t work aswell…
local DataStore = game:GetService("DataStoreService")
local LeaderboardDS = DataStore:GetDataStore("Leaderboard")
local Players = game.Players
local RE = game.ReplicatedStorage.SetInfo
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Punches = Instance.new("IntValue")
Punches.Name = "Smacks"
Punches.Value = 0
Punches.Parent = leaderstats
local Ball = Instance.new("StringValue")
Ball.Name = "Ball"
Ball.Value = "Default"
Ball.Parent = leaderstats
local suc,res = pcall(function()
return LeaderboardDS:GetAsync(player.UserId)
end)
if suc then
if res then
Punches.Value = res.Punches
Ball.Value = res.Ball
end
else
warn("Error loading score for"..player.Name.."! \n \n "..res)
end
end)
Players.PlayerRemoving:Connect(function(plr)
local leaderstats = plr.leaderstats
local data = {Punches = leaderstats.Punches.Value,Ball = leaderstats.Ball.Value}
local suc,res = pcall(function()
LeaderboardDS:SetAsync(plr.UserId,plr.HighScore.Value)
end)
if not suc then
warn("Error saving score for "..plr.Name.."! \n \n "..res)
end
end)
local DataStore = game:GetService("DataStoreService")
local LeaderboardDS = DataStore:GetDataStore("Leaderboard")
local Players = game.Players
local RE = game.ReplicatedStorage.SetInfo
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Punches = Instance.new("IntValue")
Punches.Name = "Smacks"
Punches.Value = 0
Punches.Parent = leaderstats
local Ball = Instance.new("StringValue")
Ball.Name = "Ball"
Ball.Value = "Default"
Ball.Parent = leaderstats
local suc,res = pcall(function()
return LeaderboardDS:GetAsync(player.UserId)
end)
if suc then
if res then
Punches.Value = res.Punches
Ball.Value = res.Ball
end
else
warn("Error loading score for"..player.Name.."! \n \n "..res)
end
end)
Players.PlayerRemoving:Connect(function(plr)
local leaderstats = plr.leaderstats
local data = {Punches = leaderstats.Smacks.Value,Ball = leaderstats.Ball.Value}
local suc,res = pcall(function()
LeaderboardDS:SetAsync(plr.UserId,data)
end)
if not suc then
warn("Error saving score for "..plr.Name.."! \n \n "..res)
end
end)
ProfileService is a wrap-around to the normal datastore. It adds a lot of protection and security, though I wouldn’t recommend it because it is quite advanced.
Setting up is the hard part. It requires solid understanding of how to script in order to get what you want out of it (i.e., using a ModuleScript with set, get and vel sim functions.)