Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Car Spawner Help - Delete Old Car (?)

Asked by 2 years ago

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)

2 answers

Log in to vote
0
Answered by
WoTrox 345 Moderation Voter
2 years ago
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
Ad
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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

0
Yeah well you upgraded the script a "little bit" xD WoTrox 345 — 2y
0
I made this script ready, incase they want it a different way [3 possible solutions in 1] Jay123abc2 241 — 2y
0
Also, at the 2. solution you can use "local seat = child:FindFirstChild("VehicleSeat", true)" and then check if its occupied or not, just in case the seat is not a direct child of the model WoTrox 345 — 2y
0
I have changed it, thanks! Jay123abc2 241 — 2y

Answer this question