I'm sorry, I'm new to DataStores.
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("DATA")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local tokens = Instance.new("IntValue", leaderstats)
tokens.Name = "Tokens"
tokens.Value = 0
local data
local success, errormessage pcall(function()
data = myDataStore:GetAsync(player.UserId.."-Tokens")
end)
if success ~= nil then
tokens.Value = data
else
print("There was an error while getting your data.")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-Tokens",player.leaderstats.Tokens.Value)
end)
if success then
print("Data succesfully saved.")
else
print("There was an error when saving data.")
warn(errormessage)
end
end)

1 comment
I've already enabled API services.