this is an example of an average save data and load data script
local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("datastore")
local function LoadData(player : Player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"
local intvalue = Instance.new("IntValue")
intvalue.Parent = leaderstats
intvalue.Value = 0
local success, data = pcall(function()
return datastore:GetAsync(tostring(player.UserId))
end)
if success and data then
intvalue.Value = data
print("succesfully loaded data")
else
print("failed to load data")
end
end
local function SaveData(player : Player)
local leaderstat = player:FindFirstChild("leaderstats")
local int : IntValue = leaderstat:FindFirstChild("Value")
local success, data = pcall(function()
datastore:SetAsync(tostring(player.UserId), int.Value)
end)
if success then
print("succesfully saved data")
else
print("failed to save data")
end
end
game.Players.PlayerAdded:Connect(LoadData)
game.Players.PlayerRemoving:Connect(SaveData)
let me break it down for you:
local function LoadData(player : Player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"
local intvalue = Instance.new("IntValue")
intvalue.Parent = leaderstats
intvalue.Value = 0
local success, data = pcall(function()
return datastore:GetAsync(tostring(player.UserId))
end)
if success and data then
intvalue.Value = data
print("succesfully loaded data")
else
print("failed to load data")
end
end
the function above handles and loads data safely
the code below creates a function called LoadData
local function LoadData(player : Player)
this creates the leaderboard
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"
local intvalue = Instance.new("IntValue")
intvalue.Parent = leaderstats
intvalue.Value = 0
this is the most important part
local success, data = pcall(function()
return datastore:GetAsync(tostring(player.UserId))
end)
we wrap your datastore that returns the value of the key into a pcall function and return it back to the pcall function, this makes the data variable be the value of the keys
if success and data then
intvalue.Value = data
print("succesfully loaded data")
else
print("failed to load data")
end
the if statement checks if it was a success using the first variable “success” as well as the second variable “data” and if it was a success it sets the intvalue’s value to data which is the same value as when you previously joined, the intvalue can be your coins, currency, wins or stats like the values you show in your table
lets move onto the savedata function and its similar and short to type
local function SaveData(player : Player)
local leaderstat = player:FindFirstChild("leaderstats")
local int : IntValue = leaderstat:FindFirstChild("Value")
local success, data = pcall(function()
datastore:SetAsync(tostring(player.UserId), int.Value)
end)
if success then
print("succesfully saved data")
else
print("failed to save data")
end
end
local leaderstat = player:FindFirstChild("leaderstats")
local int : IntValue = leaderstat:FindFirstChild("Value")
code aboves searches for my intvalue which is like my coins
local success, data = pcall(function()
datastore:SetAsync(tostring(player.UserId), int.Value)
end)
if success then
print("succesfully saved data")
else
print("failed to save data")
end
the datastore:SetAsync(tostring(player.UserId), int.Value) is like a method that saves data so :GetAsync() can get it
the if statement is redundant unless you want debugs
yeah so just focus on the getasync and setasync thing and we use player user id because its like your true identity
when you save data it can accept a table so saving data for each value in the table is redundant
use roblox documentation for reference not for learning