Alright, this was something I made and thought of a while back, but I figured I should post it onto resources for everyone to use. It was a simple setup for leaderstats that can be used by creating tables. You can customize the value from the table, the name of a datastore, and even whether to save a specific value. So, here is the server script (which is not what you need to change)
local LInfo = require(script.LeaderstatsSetup)
local DSS = game:GetService("DataStoreService")
game.Players.PlayerAdded:Connect(function(plr)
local data
if LInfo.DataStoreName then
data = DSS:GetDataStore(LInfo.DataStoreName):GetAsync(plr.UserId) or nil
end
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
for i = 1, #LInfo.Values do
local v = LInfo.Values[i]
local NewValue = Instance.new(v.Instance)
NewValue.Name = v.Name
if data and v.Save then
if data[v.Name] then
NewValue.Value = data[v.Name]
else
NewValue.Value = v.StartValue
end
else
NewValue.Value = v.StartValue
end
NewValue.Parent = leaderstats
end
end)
function DataSave(plr)
if LInfo.DataStoreName then
local ds = DSS:GetDataStore(LInfo.DataStoreName)
local savedata = {}
for i,v in pairs(plr.leaderstats:GetChildren()) do
if LInfo.Values[i].Name == v.Name and LInfo.Values[i].Save then
savedata[v.Name] = v.Value
end
end
ds:SetAsync(plr.UserId, savedata)
end
end
game.Players.PlayerRemoving:Connect(function(plr)
DataSave(plr)
end)
game:BindToClose(function()
for i,v in pairs(game.Players:GetPlayers()) do
DataSave(v)
end
end)
And here is the module script.
local LInfo = {
["DataStoreName"] = "TestTycoonData", --Name of datastore
["Values"] = {
[1] = {
["Instance"] = "NumberValue", --Type of Value
["StartValue"] = 0, --What the value will start out as
["Save"] = true, --Whether to save this value's data when left
["Name"] = "FirstValue" --Name of the value
},
},
}
return LInfo
The module script’s name is “LeaderstatsSetup” and is put under the server script. You simply need to customize the module to your needs. You can make more values by adding another table into the “Values” Table.
local LInfo = {
["DataStoreName"] = "TestTycoonData", --Name of datastore
["Values"] = {
[1] = {
["Instance"] = "NumberValue", --Type of Value
["StartValue"] = 0, --What the value will start out as
["Save"] = true, --Whether to save this value's data when left
["Name"] = "FirstValue" --Name of the value
},
[2] = {
["Instance"] = "BoolValue",
["StartValue"] = true,
["Save"] = false,
["Name"] = "FirstBool"
},
},
}
return LInfo
You can create as many values as you want with this.
Here is the model for it: Easy Leaderstats Setup - Roblox
Feel free to ask for any sort of function or ask any further questions on how to use it!