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

How do I despawn cars within my game? [closed]

Asked by 6 years ago

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.

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. SummerEquinox 643 — 6y

Closed as Not Constructive by IcyEvil, User#21908, 1TheNoobestNoob, User#19524, User#20388, green271, and DeceptiveCaster

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

1 answer

Log in to vote
2
Answered by 6 years ago

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
Ad