How to remove void from table/what causes it

  1. What do you want to achieve? Keep it simple and clear!
    removing void from a table/resorting indexes so there arent any empty indexes

  2. What is the issue? Include screenshots / videos if possible!
    im making a dialogue system that uses tables and the indexes in the table for it, sometimes (most of the time) it adds “void”, which breaks my script and also it sometimes just doesnt add anything but skips the index

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i looked through devforum and the api reference for tables, still dont know why this happens

this is what i expect would come out

{
[1] = "hello world"
[2] = "dialogue2"
[3] = "dialogue3"
[4] = "ok bye"
}

but sometimes it does one of these two, for example

{
[1] = "hello world"
[2] = "dialogue2"
[3] = void
[4] = "dialogue3"
[5] = "ok bye"
}

or

{
[1] = "hello world"
[2] = "dialogue2"
[3] = "dialogue3"
--comepletely skips "4" and it just isnt there
[5] = "ok bye"
}

code im using to insert the values:

local
dialogSort = {}

function insertDialog (pageVal)
--for inserting the values name and value into the table
	local pageText = pageVal.Value
	local pageName = pageVal.Name
	
	local pageNum = tonumber(pageName)

	table.insert(dialogSort, pageNum, pageText)
end
--remotevent from proximityprompt (sends a folder with stringvalues)

diaStart.OnClientEvent:Connect(function(pages)
	for i,v in pairs(pages:GetChildren()) do
		insertDialog(v)
	end
end)

ive also realized now i couldve just fired a table with the remotevent so i dont have to go through the trouble of inserting and clearing one table

but id still like an answer just incase i come by it again

any help on this would be helpful, im still learning tables so if its a mistake i did just lmk, thanks

1 Like

Can you please provide your entire script so that we can see how you are deleting the values

2 Likes

Can you please show how the table is being created/edited?

1 Like

Sorry for the necro, but I’ve recently come across this issue as well. Take a look at this code:

local t = {}
t[2] = 0
t[3] = 0
t[4] = 0
print(t)

The result is that t[1] is initialized to void. According to Lua implementation docs:
“The array part tries to store the values corresponding to integer keys from 1 to some limit n.Values corresponding to non-integer keys or to integer keys outside the array range are stored in the hash part.”

So I think Lua is recognzing your table as an array. Though we know why now, I don’t have a fix for your code unfortunately. If you still even need one.

Further support can’t really be given to OP unless they provide how they are setting up their tables (as I mentioned … 11 months ago!)

1 Like