So I have watched AlvinBlox's tutorial on Car spawning and it works fine but I have one question
how do I make it where a player can only spawn 1 car then his previous car gets deleted?
All the scripts are in his video about it im to lazy to pull up all the scripts and the hierarchy please help
The general idea is to check if the car exists. There are a few ways you can do this, however, I would recommend doing something like this:
local result, err = pcall(function() -- catches all game-breaking errors return car and car.Parent end) if result then if err == true then -- car exists car:Destroy() else -- car does not exist or parent is nil end else -- car does not exist or parent is nil end -- Car spawn logic
local function onPlayerJoin(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local money = Instance.new("IntValue") money.Name = "Money" money.Value = 22000 money.Parent = leaderstats end game.Players.PlayerAdded:Connect(onPlayerJoin) game.ReplicatedStorage:WaitForChild("CheckPrice").OnServerInvoke = function(player,NameOfCar) return game.ServerStorage.Cars:FindFirstChild(NameOfCar).Price.Value end game.ReplicatedStorage:WaitForChild("SpawnCar").OnServerEvent:Connect(function(player,NameOfCar) 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 end)