Issue with positioning

Hello guys,
I am making a custom player list for a server admin. The issue occured when two or more players come into the server. I suspect it is the positioning.

What I have now
Screenshot 2024-01-16 015759

What I want the final result to be
Screenshot 2024-01-16 015932

Code

	for _, eachPlayer in pairs(game.Players:GetPlayers()) do
		if eachPlayer then
			local textButton = Instance.new("TextButton")
			if textButton then
				textButton.Size = UDim2.new(0.274, 0,0.187, 0)
				textButton.Position += UDim2.new(.36, 0,-0.072, 0)
				textButton.Text = eachPlayer.Name
				textButton.Parent = player.PlayerGui["Admin Panel"].Admin.Options
				print(eachPlayer.Name)

				textButton.MouseButton1Down:Connect(function(hit)
					print("Hit name")
				end)
				player.PlayerGui["Admin Panel"].Admin.Options.TextButton.Visible = false
				wait(4)
				player.PlayerGui["Admin Panel"].Admin.Options.TextButton.Visible = true
				textButton:Destroy()
			end
		end
	end
1 Like

Does the TextButton actually exist? Check the Explorer to make sure that it does. If it does exist, then you can try messing with the Y-Scale of the UDim2 you’re adding onto the new TextButton’s position

I hope you are using UIListLayout or UIGridLayout for this.
List will move the GUIObjects automatically by the list’s properties.

If so, then in a scrollframe(players guis in) you would show players who are in game.
add a frame/ button by function like

local function addPlayerToList(plr:Player)
      local btn = Instance.new("TextButton")
      btn.Name = plr.Name
      btn.Parent = scrollframe -- list of players
end

and when they leave you will also need a funtion like

local function removePlayerFromList(plr:Player)
     if scrollframe:FindFirstChild(plr.Name) then
       scrollframe:FindFirstChild(plr.Name):Destroy()
     end
end

you can call these functions when a player joins / leaves and by the for loop for people who are already in the experience.

Code put together
local players = game:GetService("Players")
local scrollFrame = -- frame / list of players parent

local function addPlayerToList(plr:Player)
	if not scrollFrame:FindFirstChild(plr.Name) then
		local btn = Instance.new("TextButton")
		btn.Name = plr.Name
		btn.Parent = scrollFrame
		btn.Activated:Connect(function()
			print(btn.Name .. " button was pressed")
		end)
	end
end

local function removePlayerFromList(plr:Player)
	if scrollFrame:FindFirstChild(plr.Name) then
		scrollFrame:FindFirstChild(plr.Name):Destroy()
	end
end

for i,v in players:GetPlayers() do
	addPlayerToList(v)
end

players.PlayerAdded:Connect(addPlayerToList)
players.PlayerRemoving:Connect(removePlayerFromList)

Hello Trainmaster2341,
I took your suggestion and added in a UIGridLayout and have solved the issue but the issue now is after the TextLabel has spawned on the UI and if I click the button again, it doesn’t remove the TextLabel.

For some reason it is not calling the textLabel:Destroy() but I have added a pcall to this and it was a success.

I spent a couple of mins trying to solve this issue but so far I have no solution to solve this issue.

Screenshot 2024-01-16 113500

Code

AdminFreezeEvent.OnServerEvent:Connect(function(player)
	
	local parent = player.PlayerGui["Admin Panel"].Admin.Options
	
	for i,v in ipairs(game.Players:GetPlayers()) do
		
		local textLabel = Instance.new("TextLabel", parent)

		if v.hasJoined.Value == true then
			
			textLabel.Text = v.Name
			warn("Added a new player to the player list")
			v.hasJoined.Value = false
		else
			local success, errormessage = pcall(function()
				textLabel:Destroy()
			end)
			
			if success then
				warn("Successfully deleted")
			else
				warn("Unsuccessfully deleted")
			end
		end
	end
end)
1 Like

Yes. I have finally fixed the issue. It took me a long time. I didn’t manage to figure out the issue but after awhile I use another method.

Another thing I want to add is that I didn’t really know what the (player) mean. I recently discover this through trial and error.

(player) doesn’t mean everyone on the server but instead it refers to a specific player who fired out this event. However v refers to everyone on the server. Through coding my custom player list, this is what I found out and I always get confuse.

ezgif.com-video-to-gif-converter

Well then I suggest taking a look into Remote Events.

I’m sorry as I did not take that into account of launching for only one player.

Server Script should look something like this.

local players = game:GetService("Players")
local event = game.ReplicatedStorage:WaitForChild("RemoteEvent") -- change for your event name / location


players.PlayerAdded:Connect(function(plr:Player)
	event:FireAllClients(true,plr) -- sends bool value,and player
end)

players.PlayerRemoving:Connect(function(plr:Player)
	event:FireAllClients(false,plr) -- sends bool value,and player
end)

And Client Script should look something like this:

local event = game.ReplicatedStorage:WaitForChild("RemoteEvent") -- change for your event name / location
local Players = game:GetService("Players")

local listFrame = script.Parent.ScrollingFrame

local function checkPlayersList(joined:boolean,plr:Player)
	if joined then -- check if player joined = true / false	
		if not listFrame:FindFirstChild(plr.Name) then
			local textLabel = Instance.new("TextLabel", listFrame)
			textLabel.Name = plr.Name
		end		
	else
		local frame = listFrame:FindFirstChild(plr.Name)
		if frame then
			frame:Destroy()
			print(plr.Name .. "left")
		end
	end
end

for i,plr:Player in Players:GetChildren() do
	checkPlayersList(true,plr) -- joined = true,plr = player
end

event.OnClientEvent:Connect(checkPlayersList)

Hi Trainmaster2341,
Thank you for your response. I will play around with some stuff in the game. So far, this is how far I have gotten.

So when you press on a player name, it will show you this frame and you can select multiple options that are soon to come.

I think I will take a break for now and after work I will continue this.

1 Like

ezgif.com-video-to-gif-converter(1)

2 Likes