does anyone know how I can save this with only using setasync one time?
playerData:SetAsync(playerUserId, player.leaderstats.Number.Value)
for i, v in pairs(player.PLayerValues:GetChildren()) do
playerData:SetAsync(playerUserId, v.Value)
end
this is the whole script right now, I know its bad:
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local playerValuesTable = {"Multiplier"}
local function onPlayerJoin(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local number = Instance.new("IntValue")
number.Name = "Number"
number.Parent = leaderstats
local playerValues = Instance.new("Folder")
playerValues.Name = "PlayerValues"
playerValues.Parent = player
local playerUserId = "Player_" .. player.UserId
local data = playerData:GetAsync(playerUserId)
for index, value in pairs(playerValuesTable) do
local newValue = Instance.new("IntValue")
newValue.Name = value
newValue.Parent = playerValues
if data then
newValue.Value = data
else
newValue.Value = 0
end
end
if data then
number.Value = data
else
number.Value = 0
end
end
local function onPlayerExit(player)
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player.leaderstats.Number.Value)
for i, v in pairs(player.PLayerValues:GetChildren()) do
playerData:SetAsync(playerUserId, v.Value)
end
end)
if not success then
warn('Could not save data!')
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)