Help needed with while loop!

So I made an admin command that basically shows every admin!
How it should work is that it searches for the admin gui in every player in a loop and then adds a textlabel with their name to the admin list if it found anything. It just duplicates the textlabel over and over again and I don’t know how to make it only once! Can anyone help me out?

Edit:
Lets just say the admin only runs in one server. I can whitelist only people that don’t have it. So that means I would first have to add the one’s who are already admin. That basically means I have to update it everytime because I would only get the other people that I manually whitelisted to the admin list. So I could make a while loop out of it but I would have to delete the second generated textlabel. How can I make it so that it will checks if both of the text’s names are the same and only delete one of them instead that it deletes both?

1 Like

If you only want the loop to run once, why do you need a loop in the first place?

but try using a for loop

Because I have a second command which can whitelist anyone to the admin. I use a for loop. it just stops when it searched every player and there are still people who could join.

1 Like

You’re probably looking for a for loop, rather than a while loop.

for _,player in game:GetService("Players"):GetPlayers() do
	if player:FindFirstChild("PlayerGui") then
		if player.PlayerGui:FindFirstChild("GUI_NAME_HERE") then -- Change the name here
			-- Do your stuff...
		end
	end
end

Something like this.

In that case, use the PlayerAdded event in the Players service. (Sorry for not rereading after your edit)

game:GetService("Players").PlayerAdded:Connect(function(plr)
	if plr:WaitForChild("PlayerGui"):FindFirstChild("ADMIN_GUI_NAME_HERE") then -- Change the name here
		-- Do your stuff...
	end
end)

Not sure if this’ll work, I’m not exactly a professional with managing client-sided things.

I already use that code. I want it to be updated tho. A for loop would just stop after it finished scanning all players.

That would only work with admin’s that are build in-game.

If you want it to update when a user is whitelisted in-game you can have the for loop run when the whitelist command is run.

Lets just say the admin only runs in one server. I can whitelist only people that don’t have it. So that means I would first have to add the one’s who are already admin. That basically means I have to update it everytime because I would only get the other people that I manually whitelisted to the admin list. So I could make a while loop out of it but I would have to delete the second generated textlabel. How can I make it so that it will checks if both of the text’s names are the same and only delete one of them instead that it deletes both?

Right before you create the TextLabel, you can iterate over the Gui and check if its a TextLabel then check if its Text is equal to the name of the player you are about to create the TextLabel for and not create it.
Code example:

-- replace this code where you create a textlabel
local exists = false
for i,v in pairs(plr:WaitForChild("PlayerGui"):FindFirstChild("ADMIN_GUI_NAME_HERE")) do
	if v:IsA("TextLabel") then
		if v.Text == plr.Name then
			exists = true
		end
	end
end
if exists == false then
	-- create textlabel
end

This will iterate over the textlabels in Gui and check if a name already exists.

Apologizes for the late reply.

1 Like

Thanks bro that really helped me out!

1 Like

Correction:

-- instead of
exists == true
-- use
exists == false

I just edited the post.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.