Good day, I am looking forward to save some stats for my game, but they do not save.
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("hellou123ndet")
local stats = {
{"Coins", 100000},
{"Items", 0},
{"Inventory", nil}
}
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "stats"
folder.Parent = player
for i,v in pairs(stats) do
local statType = "IntValue"
local name = v[1]
local value = v[2]
if type(value) == "boolean" then
statType = "BoolValue"
elseif type(value) == "string" then
statType = "StringValue"
elseif value == nil then
statType = "Folder"
end
local stat = Instance.new(statType)
stat.Name = name
if value then stat.Value = value end
stat.Parent = folder
end
local data
wait(1)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-coins", player.stats.Coins.Value)
myDataStore:SetAsync(player.UserId.."-items", player.stats.Items.Value)
myDataStore:SetAsync(player.UserId.."-invry", game:GetService("HttpService"):JSONEncode(player.stats.Inventory:GetChildren()))
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-coins", player.stats.Coins.Value)
myDataStore:SetAsync(player.UserId.."-items", player.stats.Items.Value)
myDataStore:SetAsync(player.UserId.."-inventory", game:GetService("HttpService"):JSONEncode(player.stats.Inventory:GetChildren()))
end)
if success then
print("saved")
else
print("error")
print(errormessage)
end
end)
This is the whole script. There are no more scripts. Thanks for watching, double thanks for those who reply.
Edit: I have enabled API.