Table.remove or table[index] = nil

hey guys, quick question :smiley:
table.remove or table[index] = nil :thinking:

I mean;

local x = {2,3}
table.remove(x,1) or x[1] = nil

Depends.

If it’s a dictionary(or the table has specific keys), the latter is appropriate.
If it’s an array, the former.

1 Like

If you just do

that will leave a gap in the array, so it can’t really be considered an array anymore.

But table.remove shifts elements down 1 index if a gap is created, so

print(x[1])

would print 3 and not nil if you used table.remove

4 Likes

Well, I am making a matchmatching, so I have like this;

local Players = {1231231123 --[[example user ir]],21231233}
Players[1231231123] = nil -- will not work right?
or
table.remove(Players,table.find(Players,1231231123))

if you wan’t to see the code for matchmathing its wip

wip
local RepStorage = game:GetService("ReplicatedStorage")
local MessagingService = game:GetService("MessagingService")
local Datastore = game:GetService("DataStoreService")
local Matchmatching = Datastore:GetDataStore("Matchmatching")
local PlayersInside = {}
local Matches = {}
local Settings = require(script.Parent.Handler.Settings)

RepStorage.Events.EnterMatchmatching.OnServerEvent:Connect(function(Player)
	if (PlayersInside[Player]) then
		return
	end
PlayersInside[Player] = true
	MessagingService:PublishAsync('MatchmatchingEnter',Player.UserId)
end)

local function createMatch(owner)
	local suc,returned = pcall(Matchmatching.GetAsync,Matchmatching,"Matchs")	
	if (suc) then
		table.insert(returned,{Players = {owner}})
		Matchmatching:UpdateAsync("Matchs",function(x)
			return returned or x
		end)
		return returned
	end
	return nil
end

local function FindPlayerInMatch(plrID)
	local suc,returned = pcall(Matchmatching.GetAsync,Matchmatching,"Matchs")	
	if (suc) then
		if (returned) then
			for i,v in pairs(returned) do
				if (v.Players[plrID]) then
					return true
				end
			end
		end
	end
	return false
end

local function RemovePlayerFromMatch(plrID)
	local suc,returned = pcall(Matchmatching.GetAsync,Matchmatching,"Matchs")	
	if (suc) then
		if (returned) then
			for i,v in pairs(returned) do
				if (v.Players[plrID]) then
					v.Players[plrID] = nil
				end
			end
			Matchmatching:UpdateAsync("Matchs",function(x)
				return returned or x
			end)	 
		end
	end
end

local function InsertPlayerInMatch(userId,t,index)
	local suc,returned = pcall(Matchmatching.GetAsync,Matchmatching,"Matchs")	
	if (suc) then
		if (returned) then
			local Table = returned[index]
			table.insert(Table.Players,userId)
			Matchmatching:UpdateAsync("Matchs",function(x)
				return returned or x
			end)
		else

		end
	end
end

MessagingService:SubscribeAsync("MatchmatchingEnter",function(UserId)
	local suc,returned = pcall(Matchmatching.GetAsync,Matchmatching,"Matchs")	
	print(suc,returned)
	if (suc) then
		if (returned) then
			print('returned')
			table.sort(returned,
				function(a,b)
					return (a.Players < b.Players)
				end
			)
			local x = false
			for i,v in pairs(returned) do
				if (v.Players < Settings.MaxPlayers) then
					InsertPlayerInMatch(UserId,returned,i)
					x = true
					break
				end
			end
			if (not x) then
				createMatch(UserId)
			end
		else
			print('create match')
			createMatch(UserId)
		end
	end
end)
1 Like

Here’s a visual example of why table[index] = nil might not be what you want.

local t = { 1, 2, 3, 4, 5 }

t[3] = nil --> t == { 1, 2, nil, 4, 5 }
-- If you used table.remove instead, the table would turn into { 1, 2, 4, 5 }.

for index, value in ipairs(t) do
    print(value)
end
--> 1
--> 2
--> (loop stops)

table.remove will shift the remaining values after the removed index down one, to prevent the table from having nil holes in it. ipairs and # will not give you the entire contents of the array back if you have holes in your table.

7 Likes

If you don’t want to use table.remove for whatever reason, you can just replace the target value with the last value of the array.

It’s not really elegant and doesn’t achieve the same as table.remove, but will do for most applications.

local arr = {1,2,3,4,5}

arr[2] = arr[5]
arr[5] = nil
1 Like