This is a really simple leaderstats script I made for anyone to use!
When using the script there are two folder created, one is leaderstats wich every player will be able to see and the other one is a Values Folder which contains values no one can see.
In the two tables you can add as much values as you want. just type them like I did in the script and add the comma.
Script:
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
local leaderstatsTable = {
["Ball"] = 1,
}
local valuesTable = {
}
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local valuesFolder = Instance.new("Folder")
valuesFolder.Name = "Values"
valuesFolder.Parent = player
local data
local success, err = pcall(function()
data = myDataStore:GetAsync(player.UserId)
end)
for name, value in pairs(leaderstatsTable) do
local newValue = Instance.new("IntValue")
newValue.Name = name
newValue.Value = value
newValue.Parent = leaderstats
if success and data and data[name] then
newValue.Value = data[name]
end
end
for name, value in pairs(valuesTable) do
local newValue = Instance.new("IntValue")
newValue.Name = name
newValue.Value = value
newValue.Parent = valuesFolder
if success and data and data[name] then
newValue.Value = data[name]
end
end
end)
function leaderstatsToTable(leaderstatsTable)
local tbl = {}
for _, intValue in pairs(leaderstatsTable:GetChildren()) do
tbl[intValue.Name] = intValue.Value
end
return tbl
end
function valuesToTable(valuesFolder)
local tbl = {}
for _, intValue in pairs(valuesFolder:GetChildren()) do
tbl[intValue.Name] = intValue.Value
end
return tbl
end
local function dataToTable(data1,data2)
local data = {}
for i,v in pairs(data1) do
data[i] = v
end
for i,v in pairs(data2) do
data[i] = v
end
return data
end
players.PlayerRemoving:Connect(function(player)
local leaderstatsFolder = player:FindFirstChild("leaderstats")
local valuesFolder = player:FindFirstChild("Values")
if valuesFolder and leaderstatsFolder then
local dataToSave = dataToTable(leaderstatsToTable(leaderstatsFolder),valuesToTable(valuesFolder))
local success, err
local attempt = 1
repeat
success, err = pcall(function()
myDataStore:SetAsync(player.UserId, dataToSave)
end)
attempt += 1
task.wait(2)
until success or attempt > 4
if success then
print(player.Name .. "'s data was success fully saved")
else
print(player.Name .. "'s data failed to save")
end
end
end)
game:BindToClose(function()
task.wait(5)
end)
You do not have to give me credit or anithing else!