✓ Accepted answer
There is really no need to use a while loop for this problem. Instead, you can use several other methods.
When a player joins the game, check if they are the player in question
To do this, we'll utilize the PlayerAdded event of the Players service.
local players = game:GetService("Players")
local ExpectedPlayer = "xXGokyXx"
players.PlayerAdded:Connect(function(player)
if player.Name == ExpectedPlayer then
print("xXGokyXx is here!")
end
end)
You can even match this up with the PlayerRemoving event if you want to make a notification system or something of that nature.
local players = game:GetService("Players")
local ExpectedPlayer = "xXGokyXx"
players.PlayerAdded:Connect(function(player)
if player.Name == ExpectedPlayer then
print("xXGokyXx is here!")
end
end)
players.PlayerRemoving:Connect(function(player)
if player.Name == ExpectedPlayer then
print("xXGokyXx is here!")
end
end)
Scan the Players service during runtime
If you want to scan the game during its runtime (i.e. not when a player joins like we did before), you can simply use the :FindFirstChild() method like you did in your original code. Your code was technically fine, and it should have worked. You shouldn't use infinite while loops, though, unless you actually have to. My first code snippets shows a workaround for that.
##Resources:
PlayerRemoving
##Accept and upvote if this helps!
1 comment
I was trying to figure out how it worked to make another script of mine not break when someone left, I will let you know if this works!