Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My script doesn't delete the old car when a new is being spawned in?

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)

1 answer

Please try this, I used string manipulation with string:find().

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:find(game.Players.LocalPlayer.Name.."'s") then -- so what it does it if finds the players name and the apostrophe s ('s)
                        v:Destroy()
                    end
                end
            end
        end)

This discussion is preserved as it was. New answers, comments, and votes are closed.