I have been trying to learn datastore for a little while now and have reached out to other devforum posts and third party resources on discord. Unfortunately have found no luck in solving my issue.
local DSS = game:GetService("DataStoreService")
function DataTable()
local userData = {
Stats = {
Coins = 0;
Emeralds = 0;
Admin = false;
Banned = false;
};
Inventory = {
Seeds1 = 0;
Seeds2 = 0;
};
}
return userData
end
local Datastore = DSS:GetDataStore("PlayerData")
local data
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"
local success, errormessage = pcall(function()
data = Datastore:GetAsync(player.UserId) or DataTable()
local Coins = Instance.new("IntValue")
Coins.Parent = leaderstats
Coins.Value = data.Stats.Coins
Coins.Name = "Coins"
local Emeralds = Instance.new("IntValue")
Emeralds.Parent = leaderstats
Emeralds.Value = data.Stats.Emeralds
Emeralds.Name = "Emeralds"
local Admin = Instance.new("BoolValue")
Admin.Parent = leaderstats
Admin.Value = data.Stats.Admin
Admin.Name = "Admin"
local Banned = Instance.new("BoolValue")
Banned.Parent = leaderstats
Banned.Value = data.Stats.Banned
Banned.Name = "Banned"
end)
if success then
print(player.Name.. "'s Data has successfully been loaded.")
else
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
local Coins = player:WaitForChild("leaderstats").Coins
local Emeralds = player:WaitForChild("leaderstats").Emeralds
local Admin = player:WaitForChild("leaderstats").Admin
local Banned = player:WaitForChild("leaderstats").Banned
data.Stats.Coins = Coins.Value
data.Stats.Emeralds = Emeralds.Value
data.Stats.Admin = Admin.Value
data.Stats.Banned = Banned.Value
data = Datastore:SetAsync(player.UserId, DataTable())
end)
if success then
print(player.Name.. "'s Data has successfully been saved.")
else
print("Erorr")
warn(errormessage)
print(data)
end
end)
If anyone could direct me somewhere where I could learn the correct way to map data to its correct value on a table of datastore data then that would be very helpful. otherwise when I change data on the server then leave, and return in the studio the saved data isnt there.
I have checked all of these resorces already and I am stuck. particularly with the Setasync
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
local Coins = player:WaitForChild("leaderstats").Coins
local Emeralds = player:WaitForChild("leaderstats").Emeralds
local Admin = player:WaitForChild("leaderstats").Admin
local Banned = player:WaitForChild("leaderstats").Banned
data.Stats.Coins = Coins.Value
data.Stats.Emeralds = Emeralds.Value
data.Stats.Admin = Admin.Value
data.Stats.Banned = Banned.Value
data = Datastore:SetAsync(player.UserId, data)
end)
if success then
print(player.Name.. "'s Data has successfully been saved.")
else
print("Erorr")
warn(errormessage)
end
end)