I made a leaderstats script with two values : Money and wins but there seems to be an issue at line 37. If someone could send me a better/more simple leaderstats system it would be appreciated .
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MoneyStats")
local function SaveData(Player)
local Data = {
Player.leaderstats.Money.Value;
Player.leaderstats.Wins.Value
}
DataStore:SetAsync(Player.UserId, Data)
end
Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = Player
local Money = Instance.new("IntValue")
Money.Name = "Money"
Money.Parent = Leaderstats
local Wins = Instance.new("IntValue")
Wins.Name = "Wins"
Wins.Parent = Leaderstats
local Data
local Success, Error = pcall(function()
Data = DataStore:GetAsync(Player.UserId) or {}
end)
if Success then
Wins.Value = Data and Data[1] or 0
Money.Value = Data and Data[2] or 0
end
end)
Players.PlayerRemoving:Connect(SaveData)
game:BindToClose(function()
for _, Player in ipairs(Players:GetPlayers()) do
SaveData(Player)
end
end)
Data and Data[1] or 0 is an expression that results in a boolean not a number. I would change it to something like this
local Success, Error = pcall(function()
Data = DataStore:GetAsync(Player.UserId) or {0,0}
end)
if Success then
Wins.Value = Data[1]
Money.Value = Data[2]
end
function SaveData(Player)
local Data = {
["Money"] = Player.leaderstats.Money.Value;
["Wins"] = Player.leaderstats.Wins.Value;
}
DataStore:SetAsync(Player.UserId, Data)
end
and when receiving saved data you could do:
Wins.Value = 0 -- if player has no save, this will be the value of player.leaderstats.Wins.Value
Money.Value = 0
local Data
local success, Error = pcall(function()
Data = DataStore:GetAsync(Player.UserId)
end)
if Data then -- if data is in DataStore.
Money.Value = Data.Money
Wins.Value = Data.Wins
end
If the player doesn’t have any data, then the value won’t change and it will be set to 0 by default.
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MoneyStats")
local function SaveData(Player)
local Money = Player.leaderstats.Money.Value
local Wins = Player.leaderstats.Wins.Value
local Data = {Money, Wins}
local Success, Error = pcall(function()
DataStore:SetAsync(Player.UserId, Data)
end)
end
Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = Player
local Money = Instance.new("IntValue")
Money.Name = "Money"
Money.Parent = Leaderstats
local Wins = Instance.new("IntValue")
Wins.Name = "Wins"
Wins.Parent = Leaderstats
local Data
local Success, Error = pcall(function()
Data = DataStore:GetAsync(Player.UserId)
end)
if type(Data) == table then
Money.Value = Data[1]
Wins.Value = Data[2]
end
end)
Players.PlayerRemoving:Connect(SaveData)
game:BindToClose(function()
for _, Player in ipairs(Players:GetPlayers()) do
SaveData(Player)
end
end)
You should save the leader stats values individually, because using a table, you need to use table.find(), then add the table variable in it, along with the indexed number. Something like this:
local Table = table.find(Data[1],Data[2])
DataStore:SetAsync(Player.UserId, Table)
I am not the best at tables, so I am not sure if you index 1 and 2 in the same function, but if it doesn’t work, let me know.