How do I remove an old car once a new once is spawned?
script.Parent.MouseButton1Click:connect(function(GetCar)
Mod = game.ServerStorage.Cars.Van
clone = Mod:clone()
clone.Parent = workspace
clone:MakeJoints()
end)
local van = workspace:FindFirstChild("Van") --This finds the "Van" in the workspace if van then --Basically checks if the van exists (checks if the FindFirstChild has found any models) van:Destroy() end --Your script here
If you only want, 1 of the same car out at one time. This will be the code:
if workspace:FindFirstChild("Van") then workspace.Van:Destroy() end
This will simply, check for the van, if its not there in workspace. It wont run. If it is, it will just delete it.
If you want to delete it, only if nobody is sittng in it. This should work:
for i, child in pairs(workspace:GetChildren()) do if child.Name == "Van" then local chair = child:FindFirstChild("VehicleSeat", true) if chair.Occupant == nil then -- Assuming the vehicle seat is placed directly under the van model. child:Destroy() end end end
This will, check everything in the workspace. It will delete any Van that has nobody sitting in the vehicle seat.
If you want it to delete the others that player has spawned, you will require to change the code a bit.
script.Parent.MouseButton1Click:Connect(function(plr) -- GetCar is not needed, as GetCar will be shown as the Player value instead. if workspace:FindFirstChild(plr.Name.."Folder") then local folder = workspace:FindFirstChild(plr.Name.."Folder") if folder:FindFirstChild("Van") then folder.Van:Destroy() end local van = game.ServerStorage.Van:Clone() wait(0.2) van.Parent = folder local folder = workspace:FindFirstChild(plr.Name.."Folder") if folder:FindFirstChild("Van") then folder.Van:Destroy() else local folder = Instance.new("Folder") folder.Name = plr.Name.."Folder" folder.Parent = workspace local van = game.ServerStorage.Van:Clone() wait(0.2) van.Parent = folder end -- This player should have no van in this one, so it wont be required to get destroyed.
This, will make everybody a folder. This will make them there own vehicles allowed to spawn. It will delete the van inside there folder. Allowing multiple vans to be owned by multiple people!
I hope I helped, if this has a error anywhere pls tell me and I will help, if you cant figure it out. I may have also missed some ends, in some places -- sry about this