How do I make it to where I can save the money data of a player using leaderstats? It normally does not include this
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local Money = Instance.new("IntValue")
Money.Name = "Money"
Money.Value = 1000
Money.Parent = leaderstats
end)
1 Like
Why can’t you put all the information in a table and use a datastore?
Watch this and you will know: https://youtu.be/DkYupSBUpes
You can try this.
local Players = game.Players or game:GetService("Players")
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local DataBase = DataStoreService:GetDataStore("StatsData")
local DataTable = {}
local function SaveDataOnJoin(Player)
local PlayerUserId = Player.UserId
local leaderstats = Instance.new("Folder", Player)
leaderstats.Name = "leaderstats"
local Money = Instance.new("NumberValue", leaderstats)
Money.Name = "Money"
local success = nil
local playerData = nil
local Attempt = 1
repeat
success, playerData = pcall(function()
return DataBase:GetAsync(PlayerUserId)
end)
Attempt += 1
if not success then
warn(playerData)
task.wait(3)
end
until success or Attempt == 5
if success then
print(Player.Name.."'s", "data was created.")
if not playerData then
print("No data found. Did not create data.")
playerData = {
[Money] = 0,
}
end
DataTable[PlayerUserId] = playerData
else
warn("Player data could not load. Removing player...")
Player:Kick("An error occured and your data was not found. Please rejoin the game.")
end
Money.Value = DataTable[PlayerUserId].Money
Money.Changed:Connect(function()
DataTable[PlayerUserId].Money = Money.Value
end)
leaderstats.Parent = Player
end
Players.PlayerAdded:Connect(SaveDataOnJoin)
local function SaveDataOnLeave(Player)
local PlayerUserId = Player.UserId
if DataTable[PlayerUserId] then
local success = nil
local errorMsg = nil
local Attempt = 1
repeat
success, errorMsg = pcall(function()
DataBase:SetAsync(PlayerUserId, DataTable[PlayerUserId])
end)
Attempt += 1
if not success then
warn(errorMsg)
task.wait(3)
end
until success or Attempt == 5
if success then
print(Player.Name.."'s", "data saved.")
else
warn(Player.Name.."'s", "data didn't save!")
end
end
end
Players.PlayerRemoving:Connect(SaveDataOnLeave)
local function SaveDataOnServerShutDown()
if RunService:IsStudio() then
return
end
print("Server shutting down. Saving player data...")
for i, player in ipairs(Players:GetPlayers()) do
task.spawn(function()
SaveDataOnLeave(player)
end)
end
end
game:BindToClose(SaveDataOnServerShutDown)
This should hopefully prevent most data loss. This script was made by the Roblox Luau YouTube teacher known as GnomeCode. All you have to do is copy and paste the code. (Replace your old code with this code if you didn’t get what I was talking about.)
If the script doesn’t work, please let me know. I might’ve messed it up on accident, as I suck at memorizing how to save data, lol.
1 Like