Vehicle Not Despawning

So I am making a Car Spawning GUI and it was going perfectly fine until I ran into a issue I cannot fix.

So the Vehicle Spawner works but when I go to spawn a new vehicle (this should delete the old vehicle) it spawns the new one but does not delete the old one. This can make it available to the player to spawn infinity cars.

Here is the Code/Workspace:

Picture 1 - The 1st Vehicle
1

Picture 2 - The Vehicle Spawning Issue

Picture 3 - Workspace Multiple Vehicles
3

Picture 4 - ServerScriptService Script

Picture 5 - Inside the GUI

I do not understand a fix to this since I did follow a tutorial and they did not cover this.

Anything will help, Thanks!

1 Like

In this part, should not be DeleteCarEvent:FireServer(CurrentCar) instead of FindFirstChild() ?
image

So this Server event is actually called:
image

And you probably wanna use Destroy() instead of Remove() cause the last one is deprecated

2 Likes

This worked perfectly, I did find one error in the code where I called “'s Car” instead of “'s Vehicle” so that may of been another issue. Thanks so much!

1 Like

Okay pleas please PLEASE do not use his method or your map will go :dash: from exploiters, better yet ditch the naming all together and use CollectionService. Add a tag to the vehicle assigned to that specific person cause then you can keep track of that vehicle regardless if it’s name changes or it somehow moves into another parent.

I’ve used this for all my car spawning/despawning systems and highly recommended using collectionservice over directly looking for the vehicle In workspace

1 Like

Last reply I was on my phone now I’ve switched to my PC to get you a better breakdown of what I mean,


CollectionService - API Link to what I will primarily be using in this tutorial/technique type thing idk.

Moving on, for the code go ahead and made follow this script here
Do note I have not tested this!

--// LOCAL SCRIPT
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CarName = script.Parent.Name
local SpawnCarEvent = ReplicatedStorage:FindFirstChild("Spawn_Events"):FindFirstChild("SpawnCar")
local Player = game.Players.LocalPlayer

script.Parent.MouseButton1Up:Connect(function()
	SpawnCarEvent:FireServer(CarName)
	script.Parent.Parent.Parent.Parent:TweenPosition(UDim2.new(0.264, 0, -1, 0))
end)
--// SERVER SCRIPT
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SpawnCarEvent = ReplicatedStorage:FindFirstChild("Spawn_Events"):FindFirstChild("SpawnCar")
local CollectionService = game:GetService("CollectionService")

function DestroyCar(player)
	local PlayerCars = CollectionService:GetTagged(player.UserId.."_Vehicles")
	
	for _, ClearAllVehicles in pairs(PlayerCars) do
		ClearAllVehicles:Destroy() -- Remove is a deprecated function dont use it thanks <3
	end
end

SpawnCarEvent.OnServerEvent:Connect(function(Player, Car)
	local RequestedCar = ServerStorage:FindFirstChild("Vehicles"):FindFirstChild(CarName)
	
	if RequestedCar then
		DestroyCar(Player) -- Dont want to leave the man without a car if it doesn't exist
		
		local SpawnReqCar = RequestedCar:Clone()
		CollectionService:AddTag(RequestedCar, Player.UserId.."_Vehicles")
		SpawnReqCar.Parent = workspace
	end
end)

Players.PlayerRemoving:Connect(DestroyCar)
1 Like

Ok so I pasted the code and everything works except the delete function. I’m still looking at the code to see if I can fix it but I’m not sure how to.

All it does is spawns the car just like in Picture 1, 2, and 3.

In the f9 Console it points to the ServerScript line 17 where it calls CarName