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

Can i get help making this script remove a car that is already spawned when i spawn a new one?

basicly what it does that it spawns the car and i need help to make it delete the car that is already spawned if i wanna spawn another one. my idea was that i do : game.Workspace.player.Name.."'s "..NameOfCar:remove but it didnt work.

Script: `game.Players.PlayerAdded:Connect(function(plr)

local leaderstats = Instance.new("Folder")

leaderstats.Name = "leaderstats"

leaderstats.Parent = plr

local cash = Instance.new("IntValue")

cash.Name = "Cash"

cash.Value = 150000

cash.Parent = leaderstats

end)

game.ReplicatedStorage:WaitForChild("CheckPrice").OnServerInvoke = function(player,NameOfCar)

return game.ServerStorage.Cars:FindFirstChild(NameOfCar).Price.Value

end

game.ReplicatedStorage:WaitForChild("SpawnCar").OnServerEvent:Connect(function(player,NameOfCar)

local car = game.ServerStorage.Cars:FindFirstChild(NameOfCar):Clone()

car:SetPrimaryPartCFrame(player.Character.Torso.CFrame + Vector3.new(-10,0,-10))

car.Parent = workspace

car:MakeJoints()

car.Name = player.Name.."'s "..NameOfCar

end)`

1 comment

0

you ass, please post the code in the question not as a picture

1 answer

✓ Accepted answer

Try using find first child and then deleting it IF it exists:

local car = game.Workspace:FindFirstChild(player.Name.."Car")

if car then

	car:Destroy()

end

15 comments

0

Thanks for the answer but it does spawn the car and imediately deletes it, so how do i make it so it only deletes 1 if there is 2 spanwed?

0

Delete the existing car before spawning the new one

0

I’m confused. If you want to have a car that a player owns removed if it exists and then spawn a new car then you just use the code I sent and then spawn the new car after. Do I not understand?

0

but i tried putting it in front of the spawning script and adding a wait(1) and it didnt work

0

You need to post the entire code.

0

ok

0

there you go

0

?

0

The code is there

0

Is this solved now or do you still need help?

0

i still do

0

Where is the destroy command?

0

its not in it because it dosent work because i dont know where to put it in the script.

0

put the code I sent you right above this line: local car = game.ServerStorage.Cars:FindFirstChild(NameOfCar):Clone()

0

Thanks! this helped me a lot.

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