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

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

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!

2 answers

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)

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)

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