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

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

Asked by 5 years ago

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()
0
Can you take a screenshot of your Output bar and Script Analysis and post it here? Rinextel 291 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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)
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! Vibesgus 35 — 5y
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. SummerEquinox 643 — 5y
0
Yeah i understood that, I was just asking if it was possible. Vibesgus 35 — 5y
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 Vibesgus 35 — 5y
Ad
Log in to vote
0
Answered by
valchip 789 Moderation Voter
5 years ago
Edited 5 years ago

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)

Answer this question