Whenever I change the value server-side and leave the game, the data isn’t saved. How come?
Script
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
function onPlayerJoin(player) -- Runs when players join
local Interests = Instance.new("Folder") --Sets up leaderstats folder
Interests.Name = "Interests"
Interests.Parent = player
local Interest1 = Instance.new("StringValue") --Sets up value for leaderstats
Interest1.Name = "Interest1"
Interest1.Parent = Interests
local Interest2 = Instance.new("StringValue") --Sets up value for leaderstats
Interest2.Name = "Interest2"
Interest2.Parent = Interests
local Interest3 = Instance.new("StringValue") --Sets up value for leaderstats
Interest3.Name = "Interest3"
Interest3.Parent = Interests
local Interest4 = Instance.new("StringValue") --Sets up value for leaderstats
Interest4.Name = "Interest4"
Interest4.Parent = Interests
local Interest5 = Instance.new("StringValue") --Sets up value for leaderstats
Interest5.Name = "Interest5"
Interest5.Parent = Interests
local Interest6 = Instance.new("StringValue") --Sets up value for leaderstats
Interest6.Name = "Interest6"
Interest6.Parent = Interests
local Interest7 = Instance.new("StringValue") --Sets up value for leaderstats
Interest7.Name = "Interest7"
Interest7.Parent = Interests
local Interest8 = Instance.new("StringValue") --Sets up value for leaderstats
Interest8.Name = "Interest8"
Interest8.Parent = Interests
local Interest9 = Instance.new("StringValue") --Sets up value for leaderstats
Interest9.Name = "Interest9"
Interest9.Parent = Interests
local playerUserId = tostring(player.UserId).."-Interests" --Gets player ID
local data = playerData:GetAsync(playerUserId) --Checks if player has stored data
if data then
Interest1.Value = data['Interest1']
Interest2.Value = data['Interest2']
Interest3.Value = data['Interest3']
Interest4.Value = data['Interest4']
Interest5.Value = data['Interest5']
Interest6.Value = data['Interest6']
Interest7.Value = data['Interest7']
Interest8.Value = data['Interest8']
Interest9.Value = data['Interest9']
else
-- Data store is working, but no current data for this player
Interest1.Value = "None"
Interest2.Value = "None"
Interest3.Value = "None"
Interest4.Value = "None"
Interest5.Value = "None"
Interest6.Value = "None"
Interest7.Value = "None"
Interest8.Value = "None"
Interest9.Value = "None"
end
end
function create_table(player)
local player_stats = {}
for _, stat in pairs(player.Interests:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
function onPlayerExit(player) --Runs when players exit
local player_stats = create_table(player)
local success, err = pcall(function()
local playerUserId = tostring(player.UserId).."-Interests"
playerData:SetAsync(playerUserId, player_stats) --Saves player data
end)
if not success then
warn('Could not save data!')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)