Check if a player is on a server or not

I made an admin GUI and I put this script in an ImageLabel

while wait() do
	if game.Players[script.Parent.Parent.Text] == nil then
		script.Parent.ImageColor3 = Color3.new(1, 0, 0)
	else
		script.Parent.ImageColor3 = Color3.new(0.333333, 1, 0)
	end
end

The problem is , it dont work (I want to make green when the player is in and red when he is not in the server)

edit: just use local isOnServer = game:GetService("Players"):FindFirstChild(PLAYER_NAME)

I think you are better off listening for the PlayerAdded event here. Whenever a player joins, just check if their name is the same as the text to turn it green.

And you would listen for PlayerRemoving as well, doing the same; only changing the color to red if the player left.

local Players = game:GetService("Players")

local image_label = script.Parent -- reference to label

Players.PlayerAdded:Connect(function(player)
    if player.Name == image_label.Parent.Text then
        image_label.ImageColor3 = Color3.new(1/3, 0, 0)
    end
end)

Players.PlayerRemoving:Connect(function(player)
    if player.Name == image_label.Parent.Text then
        image_label.ImageColor3 = Color3.new(1, 0, 0)
    end
end)

Local Script or Normal Script lol ?

In a LocalScript, since you are handling UI.

yes , but if a Player Join when his name is not in text box it will not work ?

Can you show us the hierarchy of your UI in the Explorer window?

while wait() do
	if game.Players:FindFirstChild(script.Parent.Parent.Text) then
		script.Parent.ImageColor3 = Color3.new(1, 0, 0)
	else
		script.Parent.ImageColor3 = Color3.new(0.333333, 1, 0)
	end
end

More example:

local plrToCheck = "imAlex_30"
while wait() do
if game.Players:FindFirstChild(plrToCheck) then
print"Alex founded!"
else
print"nope"

image_2020-11-28_213052

this working :slight_smile:

while wait() do
if game.Players:FindFirstChild(script.Parent.Parent.Text) then
script.Parent.BackgroundColor3 = Color3.new(0, 1, 0.498039)
else
script.Parent.BackgroundColor3 = Color3.new(1, 0, 0)
end
end

thx everyone