Categories > Coding > Lua >
Why does my data not save???
Posted
local Players = game:GetService("Players")
local DatastoreService = game:GetService("DataStoreService")
local database = DatastoreService:GetDataStore("data")
local sessionData = {}
function PlayerAdded(player)
local Data = Instance.new("Folder")
Data.Name = "Data"
local Levels = Instance.new("IntValue")
Levels.Name = "Levels"
Levels.Parent = Data
local success = nil
local playerData = nil
local attempt = 1
repeat
success, playerData = pcall(function()
return database:GetAsync(player.UserId)
end)
attempt += 1
if not success then
warn(playerData)
task.wait(3)
end
until success or attempt == 5
if success then
print("Connected")
if not playerData then
print("Assigning Default")
playerData = {
["Levels"] = 1
}
end
sessionData[player.UserId] = playerData
else
warn("Cant Get Data")
player:Kick("Cannot Load Data")
end
Levels.Value = sessionData[player.UserId].Levels
Levels.Changed:Connect(function()
sessionData[player.UserId].Levels = Levels.Value
end)
Data.Parent = player
end
Players.PlayerAdded:Connect(PlayerAdded)
function PlayerLeaving(player)
if sessionData[player.UserId] then
local success = nil
local errorMsg = nil
local attempt = 1
repeat
success, errorMsg = pcall(function()
database:SetAsync(player.UserId, sessionData[player.UserId])
end)
attempt += 1
if not success then
warn(errorMsg)
task.wait(3)
end
until success or attempt == 5
if success then
print("Data Saved")
else
warn("Data Did Not Save")
end
end
end
Players.PlayerRemoving:Connect(PlayerLeaving)
Users viewing this thread:
( Members: 0, Guests: 1, Total: 1 )
Cancel
Post