What is the best way to tell if a player (or character) has left

While I have used the tried and true method of using a variable and having it check every time (by setting it equal to it findfirstchild’ing for the player) it comes up to a thing that handles it, I feel this is a little janky and could likely be exploited if I had too many things acting on the player. I would like to know is there a way I can make it execute the functions and have it immediately stop if it can’t find the player (without using a loop or the previously mentioned method).

The best method would be PlayerRemoving.

game.Players.PlayerRemoving:Connect(function(plr)
end)

That isn’t quite what I meant, as I’d like it to work independent of player removing (plus it there really isn’t a good way I could plug this in the way I have it set up). If you need more context on what I mean, lets say I wanted (hypothetically speaking) to teleport a person then respawn them then do whatever other stuff I’m doing, and if I were to do that I’d either have to have it check manually (by using the method in the main post) whether the player was either - still there OR if that function had returned true). This is the problem I have, I’d like to execute them without using a repeat until or having to keep manually typing for it to check a certain variable (basically like an if then that would act closer to a repeat until but not actually have it looping, as I want the if then statement to terminate the immediate time it senses that a variable changes to something. In this case it would be something like

if game.Players:FindFirstChild(plrVariable) then
do something until it either finishes the if then OR until not game.Players:FindFirstChild(plrVariable)
end

keep in mind that I DO NOT want it to loop like a repeat until would, I only want it to run the code once

I get what you mean, but there is a much easier way. You can simply have a connection outside of that and connect it to that. basically something like this.

local connections = {}

game.Players.PlayerAdded:Connect(function(plr)
    connections[plr.Name] = plr.CharacterAdded:Connect(function(char)
        print("new character")
    end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
    connections[plr.Name]:Disconnect()
end)

OR for other things like while true do s

local connections = {}

game.Players.PlayerAdded:Connect(function(plr)
    connections[plr.Name] = true
    while connections[plr.Name] do
        print("idk")
        wait(30)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    connections[plr.Name] = false
end)

The problem is, I dont want it to be terminating the entire function, I just want it to skip the if then (keep in mind the entire script is just one massive while true do (and during this time its just an if then statement doing some things to their player shortly after finishing), its not an actually a function(and I used the wrong terminology thats my bad). It’s just really hard to explain just what I mean

Let me try to make an example real quick

Well, you don’t necessarily have to terminate the function, just make it a requirement on the if then.

local connections = {}

game.Players.PlayerAdded:Connect(function(plr)
    connections[plr.Name] = true
    while true do
        if connections[plr.name] then
            print("idk")
        end
        wait(30)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    connections[plr.Name] = false
end)

So here’s the thing I had said earlier. 1. I don’t want the if then to be looping, I would just like to run those quick little snipits of code once after they have finished running 2. I also wouldn’t like it to have to check every time it goes to run them, I want it to check once then run the code inside of it and terminate it if at ANY TIME it doesn’t see the player as in the game anymore, as it could break the rest of the code in the if then if the player somehow manages to time it within it. (Like I said I will bring in an example real quickly.)

So, correct me if I’m wrong, you want it to check if the player is there, run code, and while it’s running the code, if the player leaves, the code stops right away? Honestly, I don’t think there is any way to do that specifically how you want without continuously checking almost every line of code.

Yeah, that’s basically what I want (I want the code outside of the part checking for the player to continue but otherwise spot on)

The only way I can think of is to have a function continuously running until false within a coroutine and every once in a while, checking if it has returned false, if it is nil continue, if false stop. It’s a little complicated, but I think you know what I mean.

That sounds like a good thing for me to look into. I will go try it