I got the table issue

  1. What do you want to achieve? I’m trying to get a table insert player into list. But I kept getting error. Or weird.

  2. What is the issue?

local selectedplayers = {}

function deselectplayer(player)
	if player then
		table.remove(selectedplayers, player.Name)
		print(selectedplayers)
	end
end

function selectplayer(player)
	if player then
		if table.find(selectedplayers, player.Name) then
			deselectplayer(player)
		else
			table.insert(selectedplayers, player.Name)
			print(selectedplayers)
		end
	end
end

function updatePlayers()
	for i, child in pairs(scroller:GetChildren()) do
		if child:IsA("TextButton") then
			child:Destroy()
		end
	end

	for i, plr in pairs(game.Players:GetPlayers()) do

		local btn = script.PlayerButton:Clone()

		local name = plr.Name
		btn.PlayerName.Text = name

		local image = game.Players:GetUserThumbnailAsync(plr.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size100x100)
		btn.PlayerImage.Image = image

		btn.MouseButton1Click:Connect(function()
			selectplayer(plr)
		end)

		btn.Parent = scroller
	end
end

I have printed the table but it’s not showing what I want.
image

  1. What solutions have you tried so far? I did look at my previous work that I did play with the table. Of course there’s no showing error.
1 Like

Oh, thank you. Why log mode is there?

I have still issue with removing player from table.

https://gyazo.com/48680917932b93c43ba4ed845703e151

The reason it’s erroring is that you are trying to give it a string. It needs the number of the line it is on.

So for example, if Player1 has line 1, Player2 automatically gets line 2 (if used with table.insert)

Oh. Then how it can find right player and remove it. I do use need to use in pairs to search right?

Do this:

table.remove(selectedplayers, table.find(selectedplayers, player.Name))

Table.find returns the index that the value is on, which you need for table.remove. If the value is non-existant it will return nil.

2 Likes

Thanks! I only learned this. In previous, I used a lot in pairs to find player… that’s stupid for me. Thanks.

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