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.
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!
Okay pleas please PLEASE do not use his method or your map will go 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
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)
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.