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

How do I despawn cars within my game?

Basically, I made a gui that when you press a button a car appears. I want a way so you only have one car at a time. I figured I could tag the cars by putting a StringValue.

That's as far as I went but I really need help on this, been troubling me for a few days.

1 comment

0

You could use a StringValue, or you could just name cars after the player who spawned them ie> ("Eko_h's"..CarName). Then scan workspace matching to a table or something of the sort. An even better method is probably just to keep track of what cars the player has spawned, and delete them when necessary.

1 answer

✓ Accepted answer

Your problem can be solved using a dictionary. The script checks if there already is a car that has been spawned by the player, and if there is a car, it deletes the old car and spawns the new one.

local spawnedCars = {};


function spawnCar (player, car)
	local oldCar = spawnedCars[player.UserId];
	if oldCar then spawnedCars[player.UserId]:Destroy(); spawnedCars[player.UserId] = nil; end

	spawnedCars[player.UserId] = car;
end

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