So I have this script for car spawning and there is a for loop wich detects if there is an existing car already, and deletes if so. But when I spawn another car, the old one doesn't get destroyed. Here's my script:
local cameras = workspace.Cameras
local camera = workspace.CurrentCamera
local purchasedfolder = workspace.PurchasedValues
wait(3)
if workspace.PurchasedValues.Purchasedbmw.Value == 1 then
script.Parent.Text = "Spawn"
end
script.Parent.MouseButton1Down:Connect(function()
if purchasedfolder.Purchasedbmw.Value == 1 then
game.ReplicatedStorage.SpawnCar:FireServer(script.Parent.Parent.Name)
else
local PriceOfItem = 1000
if game.Players.LocalPlayer.leaderstats.Cash.Value >= PriceOfItem and purchasedfolder.Purchasedbmw.Value == 0 then
game.ReplicatedStorage.BuyCars.Buybmw:FireServer()
game.ReplicatedStorage.ChangeValues.Changebmw:FireServer()
script.Parent.Text = "Spawn"
end
end
end)
-- Important part below
script.Parent.MouseButton1Down:connect(function()
if purchasedfolder.Purchasedbmw.Value == 1 then
for i,v in pairs(game.Workspace:GetChildren()) do
if v.Name == game.Players.LocalPlayer.Name.."'s bmw" or v.Name == game.Players.LocalPlayer.Name.."'s mc" or v.Name == game.Players.LocalPlayer.Name.."'s por" or v.Name == game.Players.LocalPlayer.Name.."'s s13" or v.Name == game.Players.LocalPlayer.Name.."'s hoon" or v.Name == game.Players.LocalPlayer.Name.."'s gtr" then
v:Destroy()
end
end
end
end)
