Getting a table's value position

--[[Say for example the player types :ban Player1 the game will run search for the player, and if it isnt nil, it will type
table.insert(BanList, PlayerName)
]]

local BanList = {"Player1", "Player2", "Player3"}

--When a player types :unban Player1, it will search the table with the following code

elseif msg:lower():sub(1,7) == Prefix.."unban " then
	local CharName = msg:lower():sub(8)
	local Length = CharName:len()
					
	for i,v in pairs(RealTimeList) do	
		if v:lower():sub(1, Length) == CharName:lower():sub(1, Length)  then
			table.remove(RealTimeList, v)
		end
	end

After this command, I realized I cannot remove tables with its string/name, I need to use an INT value, such as its table position, but how do I find a tables position in Lua?

For example if the table was like this

local Table = {"Player1", "Player2", "Player3"}

and I wanted to ban Player3, this is how I’d do it according to wiki

table.remove(Table, 3)

but the position of the name in the table is 3, so how would I detect that position?

4 Likes

Nevermind I found it, if you do a server check

local RealTimeList = {"Player1", "Player2", "Player3"}
local Count = 0
local PlayerName = "Player3"

for i,v in pairs(RealTimeList) do
	print(v)
	Count = Count+1
	if v == PlayerName then
		table.remove(RealTimeList, Count)
	end
end

You count how many players you have to cycle thru, and if your cycle stops when it finds the player in the table there you go you found the position. Just going to leave this up here if anyone else needs help.

12 Likes

Your “i” in the loop is already your “Count” variable. Use “i” in place of “Count” :wink:

7 Likes

Thank you!

2 Likes

Although already solved, this is an instance, were you’d probably want to use UserIds for banning/unbanning a user.

4 Likes