Hey alex thank you for the info
My database finally done and it works well , but im not 100% sure it will save since im still new with datastore2 , what can i add or change to make it better ?
local DataStoreService = game:GetService("DataStoreService")
local oldDataStore = DataStoreService:GetDataStore("DataSaver001") -- money DATA
local playerData = DataStoreService:GetDataStore("PlayerData") -- Vehicle DATA
local newDataStore = require(script.DataStore2)
newDataStore.Combine("DataSaver001", "PlayerData") -- combining it
--- MONEY FUNCTION OK
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
-- MOTOR FUNCTION #1
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
--MOTOR FUNCTION #2 -- MOTOR TABLE
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 = {
-- list cars put here ok
"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"
}
local function onPlayerJoin(player)
-- Money Section
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Parent = leaderstats
--- Motor Section
local leaderstats = Instance.new("Folder")
leaderstats.Name = "OwnedCars"
leaderstats.Parent = player
--- Money Section
local MoneyStats = newDataStore("MoneyData1", player)
local data = MoneyStats:Get()
if not data then
local oldData = oldDataStore:GetAsync(player.UserId.."-Money")
print("oldData for player " .. player.Name .. ": " .. tostring(oldData))
if oldData then
data = oldData
MoneyStats:Set(data)
else
warn("Error: " .. oldData)
data = {Money=3000}
end
end
if type(data) == "number" then
money.Value = data
else
money.Value = data['Money']
end
money.Changed:Connect(function(value)
MoneyStats:Set(create_table(player))
end)
-- Vehicle section ( not done yet )
local MotorStats = newDataStore("VehicleData123", player)
local data1 = MotorStats:Get()
if data1 then
for car, value in pairs(data1) do
if value == true and table.find(cars, car) then
CreateCarValue(player, car, true)
end
end
else
-- load old data
local oldmotor = playerData:GetAsync("Player_" .. player.UserId)
if oldmotor then
print("Load for player " .. player.Name)
for car, value in pairs(oldmotor) do
if value == true and table.find(cars, car) then
CreateCarValue(player, car, true)
end
end
print("Saving for player " .. player.Name)
MotorStats:Set(oldmotor)
else
print("ZZZZZ for " .. player.Name)
end
end
end
local function onPlayerExit(player)
local player_stats = create_table(player)
local motor_stats = CreateTable(player)
local MotorStats = newDataStore("VehicleData123", player)
local MoneyStats = newDataStore("MoneyData1", player)
MotorStats:Set(motor_stats)
MoneyStats:Set(player_stats)
player.leaderstats.Money.Value = player_stats.Money
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
If my script good already then tell me !