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

how can i delete a car when a player leaves the game? [closed]

Asked by 4 years ago

i'm currently using a car spawn gui, it forces you to delete your car when you want to spawn another one, but if you leave the game, the car stays there and wont delete if you rejoin, could anyone help me make it so that when a player leaves, their car gets deleted?

the naming format for the cars when spawned is (player)Car, if that can help you help me

this may come off as a request, and if you see it like that, feel free to not even answer, or just tell me if you want me to remove it

thanks in advance!

Closed as Not Constructive by AntiWorldliness, JesseSong, and TaxesArentAwesome

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?

2 answers

Log in to vote
0
Answered by 4 years ago

You could use something along the lines of

game.Players.PlayerRemoving:Connect(function(player)
    --use the delete function you have for when a player spawns a new car
end)
Ad
Log in to vote
0
Answered by 4 years ago

playerremoving is an event that fires when a player leaves the game. the code below gets ran whenever a player leaves a game because it is inside a playerremoving event.

we have the player who left as a variable, so we go through the workspace, looking at every model.

we delete anything in the workspace that has the name (playerName)Car since that is what you named the players car(s)

game.Players.PlayerRemoving:Connect(function(player)
    --find the players car and remove it from workspace
    for _, child in pairs(workspace:GetChildren())do
        --if the child we're looking at is the player's car (named (players Name)'s car), then 
        --we destroy the car
        if child.Name == player.Name.."Car" then
            child:Destroy()
        end
    end
end)