I had trouble with this too back in the days lol. Here's an example :)
local DataStore = game:GetService("DataStoreService"):GetDataStore("DataStore")
local sessionData = {}
game.Players.PlayerAdded:Connect(function(player)
local key = player.UserId
local success = false
local tries = 0
while not success and tries < 4 do
success, data = pcall(function()
return DataStore:GetAsync(player.UserId)
end)
tries = tries + 1
wait(1.5 * tries)
end
if success then
if data then
sessionData[key] = data
print("Found data for "..player.Name..".")
else
local data = {
FavoriteNums = {2, 4, 6, 10},
FavoriteLetters = {"A", "B", "D"}
}
sessionData[key] = data
print("Created data for "..player.Name..".")
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local key = player.UserId
if not sessionData[key] then return end
local success = false
local tries = 0
while not success and tries < 4 do
success = pcall(function()
DataStore:UpdateAsync(key, function()
return sessionData[key]
end)
end)
tries = tries + 1
wait(1.5 * tries)
end
if success then
print("Saved "..player.Name.."'s data".)
else
print("Unable to save "..player.Name.."'s data.")
end
sessionData[key] = nil
end)
You should probably also give this article a read: Saving Player Data.
2 comments
Fuck you nigger
^^^ what a retard swearing