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

My player existence check not printing anything?

Asked by 5 years ago

Here is the script, it is a script in ServerScriptService.

while wait(1) do
    if game.Players:FindFirstChild("xXGokyXx") == nil then
        print('xXGokyXx is not here!')
    end 
    if game.Players:FindFirstChild("xXGokyXx") ~= nil then
        print('xXGokyXx is here!')
    end 
end

It isn't printing anything when I go into team test and I'm not sure why.

1 answer

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

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:

PlayerAdded

PlayerRemoving


Accept and upvote if this helps!

0
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! xXGokyXx 46 — 5y
Ad

Answer this question