This isn’t a dictionary.
Here’s a code sample.
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local cash = Instance.new("IntValue", leaderstats)
cash.Name = "Cash"
local rebirths = Instance.new("IntValue", leaderstats)
rebirths.Name = "Rebirths"
local data = nil
local success, err = pcall(function()
data = PlayerData:GetAsync(player.UserId.."-data")
end)
if success then -- checks if grabbing data was successful
if data then -- data can be nil, so this nil check is here to check if the player has data
cash.Value = data.Cash -- looks into the dictionary created in the save, goes to the "Cash" key, and grabs the value of "Cash".
rebirths.Value = data.Rebirths -- ^ (looks for the "Rebirths" key and grabs in it's value.)
else -- here is where you'd assign default values for data, since data was nil, it's safe to assume this is a new player
cash.Value = 100 -- these are random default values, they can be whatever you wish.
rebirths.Value = 0
end
else
warn("There was an error whilst grabbing data!")
warn("Error:", err)
end
end)
Players.PlayerRemoving:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
local cash = leaderstats.Cash
local rebirths = leaderstats.Rebirths
local data = {
Cash = cash.Value, -- here we create 2 keys "Cash" and "Rebirths" to be grabbed inside of the load function
Rebirths = rebirths.Value
}
local success, err = pcall(function()
PlayerData:UpdateAsync(player.UserId.."-data", function()
return data -- saves the dictionary to the player data store.
end)
end)
if success then
print("Data saved successfully!")
else
warn("There was an error whilst saving data!")
warn("Error:", err)
end
end)