How would I make a leaderstats save in games? I have multiple places in my main game, and I want to know how I would make a leaderstats for all of them
Data Store Service. ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
I have tried that.
Script
local DSS = game:GetService("DataStoreService")
local GameData = DSS:GetDataStore("GameData")
local function saveData(player)
local StatsToSave = {
player.leaderstats.Legos.Value
}
local success1, err1 = pcall(function()
GameData:SetAsync("Player_"..player.UserId, StatsToSave)
end)
if success1 then
else
warn("error: "..err1)
end
end
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local legos = Instance.new("IntValue")
legos.Name = "Legos"
legos.Parent = leaderstats
local data
local success, err = pcall(function()
data = GameData:GetAsync("Player_"..plr.UserId)
end)
if success and data then
legos.Value = data[1]
print(data[1])
else
print("The player has no data!")
end
end)
--game.Players.PlayerRemoving:Connect(function(player)
--local success, err = pcall(function()
--saveData(player)
--end)
--if success then
--print("Saved")
--else
--warn("Data has not been saved!")
--end
--end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
local success2, err2 = pcall(function()
saveData(player)
end)
if success2 then
print("Saved")
else
warn("error : "..err2)
end
end
end)
And you’re using :GetAsync() on the other places using the same DataStore correct? (‘GameData’)
Yeah I pasted the same script in the other games
Are you getting any errors when running?
no, it just doesn’t save when I go to another game only in that game.
Have you tried printing out the data table to check its correct?
no how would I do that, I didn’t make this script I have someone helping me not online currently
When you get the data, you can use
print(data)
and if you’re running in studio it’ll print the table and you can view the values
This should be working.
local GameData = DSS:GetDataStore("GameData")
local function saveData(player)
local StatsToSave = {
["LegosCount"] = player.leaderstats.Legos.Value
}
local success1, err1 = pcall(function()
GameData:SetAsync("Player_"..player.UserId, StatsToSave)
end)
if success1 then
else
warn("error: "..err1)
end
end
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local legos = Instance.new("IntValue")
legos.Name = "Legos"
legos.Parent = leaderstats
local data
local success, err = pcall(function()
data = GameData:GetAsync("Player_"..plr.UserId)
end)
if success and data then
legos.Value = data.LegosCount
print(data.LegosCount)
else
print(err) -- prints error message
end
-- legos.Value = 1 (for testing)
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
saveData(player)
end)
if success then
print("Saved")
else
warn("Data has not been saved!")
end
end)
function SavePlayers()
for _, player in pairs(game.Players:GetPlayers()) do
saveData(player)
end
end
game:BindToClose(SavePlayers)```
If the places are part of the same universe, users datastore data will be shared across the places. Although if each place is part of a different universe, you will have to use some kind of external datastore using HttpService.
The problem was that it was like this
-- game:BindToClose(function()
-- for _, player in pairs(game.Players:GetPlayers()) do
-- local success2, err2 = pcall(function()
-- saveData(player)
-- end)
-- if success2 then
-- print("Save")
-- else
-- warn("error : "..err2)
-- end
-- end
-- end)
sorry im not really smart when it comes to scripting…
local Players = game:GetService("Players")
local DataStores = game:GetService("DataStoreService")
local DataStore = DataStores:GetDataStore("GameData")
local ProtectedCall = pcall
local function SaveData(Player)
local Success, Result = ProtectedCall(function()
return DataStore:SetAsync("Player_"..Player.UserId, {Player.leaderstats.Legos.Value})
end)
if Success then
print(Result)
else
warn(Result)
end
end
Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = Player
local Legos = Instance.new("IntValue")
Legos.Name = "Legos"
Legos.Parent = Leaderstats
local Success, Result = ProtectedCall(function()
return DataStore:GetAsync("Player_"..Player.UserId)
end)
if Success then
print(Result)
Legos.Value = Result[1]
else
warn(Result)
end
end)
Players.PlayerRemoving:Connect(function(Player)
SaveData(Player)
end)
game:BindToClose(function()
for _, Player in ipairs(Players:GetPlayers()) do
SaveData(Player)
end
end)