Table concat and numbers

I’m trying to “unpack” a table with numbers but separate them with commas for argument purposes to pass, like this. However it returns nil due to the comma.

function Engine:Fire(event:string?, ch, ...):BindableEvent?
-- ... in this case is {15,2}
	if event:lower():match("fire") then
		_G:ReP(ch.Character, "ev").Burn:Fire(tonumber(table.concat(..., ","))) -- Burn events takes two arguments as number, (10,5), like this.
	end
end
1 Like

The comma is not a number. You cannot use tonumber on a string that contains non-numeric characters.

Also, even if you removed tonumber, you would not be passing two arguments. You would be passing the string “10,5” as one argument.

You must explicitly put a comma in the parentheses between two values yourself, there is no jank way to do it like you’re trying to do it in a single line. Slow down and spread your operations out, don’t cram them all into a single line.

1 Like

If you want to “unpack” a table, you can use the table.unpack method.

table.unpack(...)

And if your tuple (…) is just the numbers alone, you’re able to just pass that instead.
Otherwise, you should just use table.unpack.

And if you want a string of numbers, you can just pass the table.concat with no tonumber method.

Last time I tried this

local d = {1,2}
local t = table.unpack(d)
print(t)

Only 1 printed.

Yeah, because you’re only assigning the first value of the table to one variable. You need to assign multiple variables.

t, p = table.unpack(d)

Sorry, was a bit busy, but yes, as biglulu stated, you’re assigning a tuple to a variable, which only takes as many values as you put variables.

The proper way to use this would be in a method like :Fire(table.unpack(…))

local T = {4,3,2,1}
local function FakeFire(...)
  local Values = {...} -- We can recompile the tuple (...) to a table with curly braces.
  for Which, Value in pairs({...}) do -- We can also iterate the values
    print(Which, Value) -- And then print/use them.
  end
  print(Values)
end

-- Let's simulate :Fire() by calling FakeFire()

FakeFire(table.unpack(T))

-- There you have it.

This worked but is there any way to separate the two with commas so i can pass them as args like I said?
As of @IDoLua, your method worked but on the bindable event function itself it errored Unable to cast Array to float

Burn.Event:Connect(function(Time:number?, Damage:number?)
	Humanoid:TakeDamage(Damage or 5) -- Errored.
	_G:ReP(Character, "va").BurningTime.Value = Time
end)

Here you go. Keep in mind this only works if the first number and second number are passed in as the first and second argument after ch.

function Engine:Fire(event:string?, ch, ...):BindableEvent?
-- ... in this case is {15,2}
	if event:lower():match("fire") then
        local arg = {...}
        local num1, num2 = table.unpack(arg)
		_G:ReP(ch.Character, "ev").Burn:Fire(num1, num2) -- Burn events takes two arguments as number, (10,5), like this.
	end
end

... is not a table, if you want a table, use {...}.

1 Like

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