Hey, my script works fine, like really fine, but somehow 1 out of 100 people will experience a data loss. but once I checked his data by using the datastore editor, he does have his data. So I assumed the data failed to load. How to prevent by using repeat inside get()?
local DataStore2 = require(script.DataStore2)
local Ds2Vehicle = "VehicleDataStoreIrfan"
local function CreateCarValue(player, name, value)
local carValue = player.OwnedCars:FindFirstChild(name)
if not carValue then
carValue = Instance.new("BoolValue")
carValue.Name = name
carValue.Parent = player.OwnedCars
end
carValue.Value = value
return carValue
end
local function VehicleFunction(player)
local MotorStats = DataStore2(Ds2Vehicle, player)
local data = MotorStats:Get()
if data then
print("Vehicle data loaded for " .. player.Name)
for car, value in pairs(data) do
if value == true then
CreateCarValue(player, car, true)
end
end
end
end
local function SaveData(player)
local motorStats = {}
for _, stat in pairs(player.OwnedCars:GetChildren()) do
if stat.Value == true then
motorStats[stat.Name] = true
end
end
local MotorStats = DataStore2(Ds2Vehicle, player)
MotorStats:Set(motorStats)
end
local function ManualSave(player)
local motorStats = {}
for _, stat in pairs(player.OwnedCars:GetChildren()) do
if stat.Value == true then
motorStats[stat.Name] = true
end
end
local MotorStats = DataStore2(Ds2Vehicle, player)
MotorStats:Set(motorStats)
local numCarsOwned = 0
for _, value in pairs(motorStats) do
if value == true then
numCarsOwned = numCarsOwned + 1
end
end
print("Number of booleans owned by " .. player.Name .. ": " .. numCarsOwned)
local remoteEvent = game.ReplicatedStorage.SendOutput
remoteEvent:FireClient(player, numCarsOwned)
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "OwnedCars"
leaderstats.Parent = player
VehicleFunction(player)
end)
game.ReplicatedStorage.DataSave.Event:Connect(function(player)
-- important --
SaveData(player)
print("Saved bought from " .. player.Name)
end)
game.ReplicatedStorage.ManualSave.OnServerEvent:Connect(function(player)
-- for influencer only --
ManualSave(player)
end)