Hello right now how my datastore is set up when players leave they lose the car they bought. How could i make this not happen. You buy the cars in a gui.
Datastore
local Players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local datastore = game:GetService("DataStoreService")
local dsl = datastore:GetDataStore("StageSaveSystem")
local DebounceTable = {}
Players.PlayerAdded:Connect(function(plr)
DebounceTable[plr] = {debounce = false}
local folder =Instance.new("Folder", plr)
folder.Name = "leaderstats"
local Cash = Instance.new("IntValue", folder)
Cash.Name = "Cash"
Cash.Value = dsl:GetAsync(plr.UserId) or 200
local success, errorMessage = pcall(function()
dsl:SetAsync(plr.UserId, Cash.Value)
end)
if success then
Cash.Changed:Connect(function()
local success, errorMessage = pcall(function()
dsl:SetAsync(plr.UserId, Cash.Value)
end)
if not success then
warn(errorMessage)
end
end)
else
warn(errorMessage)
end
end)
Players.PlayerRemoving:Connect(function(plr)
DebounceTable[plr] = nil
end)
replicatedStorage:WaitForChild("CheckPrice").OnServerInvoke = function(player,NameOfCar)
return game.ServerStorage.Cars:FindFirstChild(NameOfCar).Price.Value
end
replicatedStorage:WaitForChild("SpawnCar").OnServerEvent:Connect(function(player,NameOfCar)
if DebounceTable[player]["debounce"] then
return
end
DebounceTable[player]["debounce"] = true
if workspace:FindFirstChild(player.Name .. "'s " .. NameOfCar) then
workspace:FindFirstChild(player.Name .. "'s " .. NameOfCar):Destroy()
end
local car = game.ServerStorage.Cars:FindFirstChild(NameOfCar):Clone()
car:SetPrimaryPartCFrame(player.Character.HumanoidRootPart.CFrame + Vector3.new(0,0,15))
car.Parent = workspace
car:MakeJoints()
car.Name = (player.Name .. "'s " .. NameOfCar)
wait(5) -- Cooldown
DebounceTable[player]["debounce"] = false
end)