Hi everyone. I have followed a datatores tutorial because I’m not familiar with them, but when I finished the script, my data did not save. I have tried in studio and in-game, enabled api services but nothing worked. I rewatched the tutorial, before eventually turning to the forum.
local dss = game:GetService("DataStoreService")
local playerdata = dss:GetDataStore("MyDataStore")
local function playerjoin(player)
local leaderstats = player:WaitForChild("leaderstats")
local copper = leaderstats:WaitForChild("Copper")
local silver = leaderstats:WaitForChild("Silver")
local gold = leaderstats:WaitForChild("Gold")
local multiplier = leaderstats:WaitForChild("Multiplier")
local playeruserid = "Player_"..player.UserId
local data = playerdata:GetChildren(playeruserid)
if data then
multiplier.Value = data["Multiplier"]
copper.Value = data["Copper"]
silver.Value = data["Silver"]
gold.Value = data["Gold"]
else
multiplier.Value = 0
copper.Value = 0
silver.Value = 0
gold.Value = 0
end
end
local function createtable(player)
local playerstats = {}
for i, stat in pairs(player.leaderstats:GetChildren()) do
playerstats[stat.Name] = stat.Value
end
return playerstats
end
local function playerexit(player)
local playerstats = createtable(player)
local success, err = pcall(function()
local playeruserid = "Player_"..player.UserId
playerdata:SetAsync(playeruserid, playerstats)
end)
if not success then
warn("Data save unsuccessful!")
end
end
game.Players.PlayerAdded:Connect(playerjoin)
game.Players.PlayerRemoving:Connect(playerexit)
I also have another script that waits when the game is closing but that isn’t part of the issue. Thanks so much to whoever solves this!
Idk why I would have written getchildren lol must have been an autocorrect error. Also, I did say that i was following a tutorial. I don’t know what update async does but I just wrote what he wrote