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

Help me make this script run when player leaves the game [Easy to fix?]

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()

1 comment

0

Can you take a screenshot of your Output bar and Script Analysis and post it here?

2 answers

✓ Accepted answer

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)

4 comments

0

Thanks! it worked very well. Oh and Im new to scripting and its hard for me to learn. I always forget, and there is so much to learn, I only know the basics. These types of scripts are too hard for me so I am really lost, but I am grateful i found this site, it really helps me a lot!

0

That is fine - but keep in mind scriptinghelpers is not a request-driven site. It is by scripters, for scripting, to help you learn and progress.

0

Yeah i understood that, I was just asking if it was possible.

0

Also can you help me modify this script since all cars are named (player.Name .. "'s" .. NameOfCar) and my already existing script wont work with that kind of name. So i am asking for help, only you can help me https://pastebin.com/3NdFHTyA

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)

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