Hi. I was making a game, but then I realized that my save script doesn't seem to save specifically to each player. Rather, it saves one piece of data and then loads it for every player. Could I get a little help? Code:
local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerData")
--Saving Stats
game.Players.PlayerRemoving:connect(function(player)
player:WaitForDataReady() -- Make sure we aren't looking for leaderstats before they are created
wait(2) -- Just in case
local stats = player:FindFirstChild("leaderstats"):GetChildren()
for i = 1, #stats do -- creates a loop for all the stats
print(stats[i].Name)
datastore:SetAsync(stats[i].Name.. player.userId, stats[i].Value)
end
end)
-- Loading Stats
game.Players.PlayerAdded:connect(function(player)
player:WaitForDataReady() -- Make sure we aren't looking for leaderstats before they are created
wait(2) -- Just in case
local stats2 = player:FindFirstChild("leaderstats"):GetChildren()
for i = 1, #stats2 do
stats2[i].Value = datastore:GetAsync(stats2[i].Name)
end
end)
game.OnClose = function()
print("Saving time!")
wait(10)
print("We should have saved now!")
end |