How do you make a script run once player leaves the game?
I want to make this script run when I leave the game
local theOldCar=workspace:FindFirstChild(player.Name .. "'s" .. NameOfCar) if theOldCar~=nil then theOldCar:Destroy()
When a player leaves, have the PlayerRemoving function ready on the server. Have it catch which player is leaving - and remove their cars. This is the same question I recommended you give some effort to before asking for help with.
game.Players.PlayerRemoving:Connect(function(plr) local Names = { plr.Name .. "'sVan"; plr.Name .. "'sCar"; plr.Name .. "'sOff-Roader"; plr.Name .. "'sArmy Truck" } for _,v in next,game.Workspace:GetChildren() do if v:IsA("Model") then for i = 1,#Names do if v ~= nil and v.Name == Names[i] then v:Destroy() end end end end end)
There is an article about this, click here to visit it.
So basicly what you can use is an event called .PlayerRemoving
.
Example:
game.Players.PlayerRemoving:Connect(function(plr) print(plr.Name.." has left the game") end)