Hey , i dont know but i tried my best to prevent from dataloses but it still happen and yet i dont know how to counter it , heres my scritp
local DataStoreService = game:GetService("DataStoreService")
local DataStore2 = require(script.DataStore2)
local Ds2Vehicle = "VehicleDataStoreIrfan" -- do not change it
local playerData = DataStoreService:GetDataStore("PlayerData")
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 CreateTable(player)
local playerStats = {}
for _, stat in pairs(player.OwnedCars:GetChildren()) do
if stat.Value == true then
playerStats[stat.Name] = true
end
end
return playerStats
end
-- MOTOR FUNCTION #3
local cars = {
"HondeEX5",
"HondeWave100",
"Kasaki 150sp",
"Lagenda115zr",
"Lajak",
"Rzx Milinium",
"Wave125i Drag",
"Light Lz",
"Yamha Soleriz",
"Yamha 125rz",
"Sniper150Drag",
"Yamha Sniper135 v1",
"Honde Deo",
"Honde Nsr500",
"Icikiwir",
"Yamha Yzr500",
"Honde Wave 125r",
"Kasaki Pdk R1",
"Dukatti SuperSport S",
"Kasaki Serpico",
"Yamha X1r",
"Vesp 150ss",
"Dukatti 1299",
"Yamha Sniper v2",
"Suzuka Raider 150",
"Kasaki Ninja H2",
"Yamha R6 v2",
"Suzuka Hayabusa Drag",
"Yamha X1r White",
"Mz Agusza F4",
"Honde NR750",
"Dukatti 1199 Martini"
}
local function VehicleFunction(player)
local MotorStats = DataStore2(Ds2Vehicle, player)
local data1 = MotorStats:Get()
if data1 then
print("Vehicle data loaded for " .. player.Name)
for car, value in pairs(data1) do
if value == true and table.find(cars, car) then
CreateCarValue(player, car, true)
end
end
else
local attempt = 1
local success = nil
local data = nil
repeat
success, data = pcall(function()
return playerData:GetAsync("Player_" .. player.UserId)
end)
attempt += 1
if not success then
warn(data)
wait(1)
end
until success or attempt == 5
if data then
print("Load old vehicle data for player " .. player.Name)
for car, value in pairs(data) do
if value == true and table.find(cars, car) then
CreateCarValue(player, car, true)
end
end
print("Saving for player " .. player.Name)
MotorStats:Set(data)
else
warn("Unable to get vehicle data for " .. player.UserId)
player:Kick("Unable to load your data, please wait for 10 seconds and rejoin !")
end
end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "OwnedCars"
leaderstats.Parent = player
VehicleFunction(player)
end)
local function SavePlayerData(player)
local motorStats = CreateTable(player)
local MotorStats = DataStore2(Ds2Vehicle, player)
MotorStats:Set(motorStats)
print("Vehicle data saved for " .. player.Name)
-- pretty sure it will not damage the data just want to check the amount of booleans
local numCarsOwned = 0
for _, value in pairs(motorStats) do
if value == true then
numCarsOwned = numCarsOwned + 1
end
end
print("Number of boolean owned by " .. player.Name .. ": " .. numCarsOwned)
end
game.Players.PlayerRemoving:Connect(function(player) -- player leave
SavePlayerData(player)
end)
game.ReplicatedStorage.DataSave.Event:Connect(function(player) -- everytime player bought vehicle it i will fire
SavePlayerData(player)
wait(0.5)
print("Saved bought from " .. player.Name)
end)
game.ReplicatedStorage.ManualSave.OnServerEvent:Connect(function(player) -- manual save
SavePlayerData(player)
wait(0.5)
print("Manual data saved" .. player.Name)
end)