Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago
Edited 5 years ago

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)`

0
you ass, please post the code in the question not as a picture User#24403 69 — 5y

1 answer

Log in to vote
0
Answered by
vissequ 105
5 years ago

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
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? DaggerSaber 14 — 5y
0
Delete the existing car before spawning the new one vissequ 105 — 5y
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? vissequ 105 — 5y
0
but i tried putting it in front of the spawning script and adding a wait(1) and it didnt work DaggerSaber 14 — 5y
View all comments (11 more)
0
You need to post the entire code. vissequ 105 — 5y
0
ok DaggerSaber 14 — 5y
0
there you go DaggerSaber 14 — 5y
0
? vissequ 105 — 5y
0
The code is there DaggerSaber 14 — 5y
0
Is this solved now or do you still need help? vissequ 105 — 5y
0
i still do DaggerSaber 14 — 5y
0
Where is the destroy command? vissequ 105 — 5y
0
its not in it because it dosent work because i dont know where to put it in the script. DaggerSaber 14 — 5y
0
put the code I sent you right above this line: local car = game.ServerStorage.Cars:FindFirstChild(NameOfCar):Clone() vissequ 105 — 5y
0
Thanks! this helped me a lot. DaggerSaber 14 — 5y
Ad

Answer this question