How to get a parameter using a for loop in a function

You can write your topic however you want, but you need to answer these questions:
Haven’t used the forum in a while and my lua might have gotten a little ‘rusty’
Sorry for the lack of clarification in the title, hard for me to explain what the issue is:

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to compute the ‘output’ of my neural network and i’m trying to use a neural network to ‘combine’ a letter(isn’t defined) and i that should give in theory the parameter.

  2. When i try to combine a letter and ‘i’ using a for loop(that should make my parameter) it doesn’t work, simply tells me:
    09:56:19.304 ServerScriptService.Script:55: attempt to perform arithmetic (mul) on nil - Server - Script:55
    Here is my current code:

function computeoutput(outputneuron, w1,w2,w3,w4,w5,w6,w7,w8,w9,w10,w11,w12,w13,w14,w15,w16,w17,w18,w19,w20,w21,w22,w23,w24,w25,w26,w27,w28,w29,w30,w31,w32,w33,w34,w35,w36,w37,w38,w39,w40,w41,w42,w43,w44,w45,w46,w47,w48,w49,w50)
	local storage = 0
	local h = _G.hiddenNeurons_1
	for i = 1, 50 do
		storage = storage + h[i] * w..i -- where the error is occuring
	end
	storage = activation(storage)
	table.insert(_G.outputneurons_1, storage)
	return storage
end
  1. I’ve tried looking on developer hub but have found no documentation on this, i’ve even tried ChatGPT and its solutions don’t work.

Here is the entire script(server side in server script service):

-- Define a global weight variable
_G.weight_1 = {}
_G.hiddenNeurons_1 = {}
_G.inputneurons_1 = {}
_G.biases = {0.2,0.4}
_G.outputneurons_1 = {}

-- variables for later
local j = 0
local m = 0
local d = 801
local f = 0


-- functions
function generateinputs_1_1()
	local ai = workspace.Simulation1.AI1.Position
	local other = workspace.Simulation1
	table.insert(_G.inputneurons_1,ai.X - other.AI2.Position.X)
	table.insert(_G.inputneurons_1, workspace.Simulation1.AI1.Position.Z - workspace.Simulation1.AI2.Position.Z)
	table.insert(_G.inputneurons_1, workspace.Simulation1.AI1.Position.X - workspace.Simulation1.AI3.Position.X)
	table.insert(_G.inputneurons_1, workspace.Simulation1.AI1.Position.Z - workspace.Simulation1.AI3.Position.Z)
	table.insert(_G.inputneurons_1, workspace.Simulation1.AI1.Position.X - workspace.Simulation1.AI4.Position.X)
	table.insert(_G.inputneurons_1, workspace.Simulation1.AI1.Position.Z - workspace.Simulation1.AI4.Position.Z)
	table.insert(_G.inputneurons_1, ai.X - other.Wall1.Position.X)
	table.insert(_G.inputneurons_1, ai.Z - other.Wall1.Position.Z)
	table.insert(_G.inputneurons_1, ai.X - other.Wall2.Position.X)
	table.insert(_G.inputneurons_1, ai.Z - other.Wall2.Position.Z)
	table.insert(_G.inputneurons_1, ai.X - other.Wall3.Position.X)
	table.insert(_G.inputneurons_1, ai.Z - other.Wall3.Position.Z)
	table.insert(_G.inputneurons_1, ai.X - other.Wall4.Position.X)
	table.insert(_G.inputneurons_1, ai.Z - other.Wall4.Position.Z)
	table.insert(_G.inputneurons_1, ai.X - other.Food.Position.X)
	table.insert(_G.inputneurons_1, ai.Z - other.Food.Position.Z)
end

-- Function to generate random weights
function generateWeights(numWeights)
	for i = 1, numWeights do
		table.insert(_G.weight_1, math.random() - 0.5)
	end
end
local function activation(x)
	return math.tanh(x)
end
generateinputs_1_1()
generateWeights(2000)
function computehiddenlayer(hiddenneuron, w1,w2,w3,w4,w5,w6,w7,w8,w9,w10,w11,w12,w13,w14,w15,w16,biasnum)
	local storage = 0
	storage = _G.inputneurons_1[1]*w1+_G.inputneurons_1[2]*w2+_G.inputneurons_1[3]*w3 + _G.inputneurons_1[4]*w4+_G.inputneurons_1[5]*w5+_G.inputneurons_1[6]*w6 + _G.inputneurons_1[7]*w7+_G.inputneurons_1[8]*w8+_G.inputneurons_1[9]*w9 + _G.inputneurons_1[10]*w10+_G.inputneurons_1[11]*w11+_G.inputneurons_1[12]*w12 + _G.inputneurons_1[13]*w13+_G.inputneurons_1[14]*w14+_G.inputneurons_1[15]*w15 + _G.inputneurons_1[16]*w16+_G.biases[biasnum]
	table.insert(_G.inputneurons_1, activation(storage))
	print(_G.inputneurons_1[hiddenneuron])
end
function computeoutput(outputneuron, w1,w2,w3,w4,w5,w6,w7,w8,w9,w10,w11,w12,w13,w14,w15,w16,w17,w18,w19,w20,w21,w22,w23,w24,w25,w26,w27,w28,w29,w30,w31,w32,w33,w34,w35,w36,w37,w38,w39,w40,w41,w42,w43,w44,w45,w46,w47,w48,w49,w50)
	local storage = 0
	local h = _G.hiddenNeurons_1
	for i = 1, 50 do
		storage = storage + h[i] * w..i
	end
	storage = activation(storage)
	table.insert(_G.outputneurons_1, storage)
	return storage
end
for i = 0, 800 do
	if i /16 == math.floor(i/16) then
		m += 1
		j +=1
		print(j)
		computehiddenlayer(j,_G.weight_1[m],_G.weight_1[m+1],_G.weight_1[m+2],_G.weight_1[m+3],_G.weight_1[m+4],_G.weight_1[m+5],_G.weight_1[m+6],_G.weight_1[m+7],_G.weight_1[m+8],_G.weight_1[m+9],_G.weight_1[m+10],_G.weight_1[m+11],_G.weight_1[m+12],_G.weight_1[m+13],_G.weight_1[m+14],_G.weight_1[m+15],1)
		m+=15

		i+=1
	else
		i +=1
	end
end

for i = 0, 2 do
	local t = _G.weight_1
	computeoutput(i,t[d],t[d+1],t[d+2],t[d+3],t[d+4],t[d+5],t[d+6],t[d+7],t[d+8],t[d+9],t[d+10],t[d+11],t[d+12],t[d+13],t[d+14],t[d+15],t[d+16],t[d+17],t[d+18],t[d+19],t[d+20],t[d+21],t[d+22],t[d+23],t[d+24],t[d+25],t[d+26],t[d+27],t[d+28],t[d+29],t[d+30],t[d+31],t[d+31],t[d+32],t[d+33],t[d+34],t[d+35],t[d+36],t[d+37],t[d+38],t[d+39],t[d+40],t[d+41],t[d+42],t[d+43],t[d+44],t[d+45],t[d+46],t[d+47],t[d+48],t[d+49])
	d += 50

end
for i = 1, #_G.weight_1 do
	print("Weight " .. i .. ": " .. _G.weight_1[i])
end



sorry that the code isn’t neat and well organized

Idk why you are using that much of parameters while you can just make them in a table of argumanets like this:

function computeoutput(outputneuron, ...)
	local args = {...}
	local storage = 0
	local h = _G.hiddenNeurons_1
	for i = 1, 50 do
		storage = storage + h[i] * args[i] -- that should solve the error right?
	end
	storage = activation(storage)
	table.insert(_G.outputneurons_1, storage)
	return storage
end

The error message “attempt to perform arithmetic (mul) on nil” means that you are trying to multiply a nil value with some other value. The .. operator is used to concatenate strings and not to concatenate a variable name and a number. Therefore, using w..i will not result in the desired effect.

To fix the error, you can use the following code instead of storage = storage + h[i] * w..i:

storage = storage + h[i] * _G["w" .. i]

This will concatenate the string “w” with the value of i and use the resulting string to access the global variable with the same name.

Here is the updated computeoutput function:

function computeoutput(outputneuron, w1,w2,w3,w4,w5,w6,w7,w8,w9,w10,w11,w12,w13,w14,w15,w16,w17,w18,w19,w20,w21,w22,w23,w24,w25,w26,w27,w28,w29,w30,w31,w32,w33,w34,w35,w36,w37,w38,w39,w40,w41,w42,w43,w44,w45,w46,w47,w48,w49,w50)
	local storage = 0
	local h = _G.hiddenNeurons_1
	for i = 1, 50 do
		storage = storage + h[i] * _G["w" .. i]
	end
	storage = activation(storage)
	table.insert(_G.outputneurons_1, storage)
	return storage
end