Maxscript Beginner issues with index forloop

hello I started maxscript on MAX 2024 a little while ago and I wanted to create my bones chain from point
and I did but there is a problem in my code
when , in the index forloop I told him to choose the second information from my array he answer me with an error that I don’t understand ( pos is undefined , and he still applies the code haha )

here is my code

POS_BODY = #()

rollout myRollout “Create Bones”
(
button createButtonPoint “Create Point”
button createButtonBones “Create Point”

on createButtonPoint pressed do

POS_BODY = #(
( PRE_SHOULDER = sphere pos: [-4.51,-3,180.131] wirecolor:red radius:1.5),
( SHOULDER = sphere pos: [-19.39,-0.013,176.084] wirecolor:red radius:1.5),
( ELBOW = sphere pos: [-47.155,2.628,163.611] wirecolor:red radius:1.5 ),
( FORARM = sphere pos: [-72.66,-0.699,152.156] wirecolor:red radius:1.5 ),
( HAND = sphere pos: [-78.037,-1.401,149.741] wirecolor:red radius:1.5 )
  
	)

FN bonetest bar =

	for i = 1 to bar.count do 
	

	(
	boneA = boneSys.createBone bar[i].pos bar[(i)+1].pos [1,0,0] 
	)


on createButtonBones pressed do
	
bonetest POS_BODY

)

– Créer la fenêtreb
createDialog myRollout 300 60

does anyone have a solution and any advice on how to save me from having to deal with these kinds of little problems, which for me are not little problems haha.

try(destroydialog rol) catch()
rollout rol "Create Bones" width:200
(
	struct my_bone 
	(
		name = "",
		size = 1.5,
		pos = [0,0,0],
		wirecolor = red
	)
	
	button createButtonPoint "Create Points" width:180 align:#center
	button createButtonBones "Create Bones Using Data" width:180 align:#center
	button createButtonBones2 "Create Bones Using Points" width:180 align:#center

	local POS_BODY
	local PRE_SHOULDER 	= my_bone name:"PRE_SHOULDER" 	size:10 pos:[-4.51,-3,180.131] 	     wirecolor:red 
 	local SHOULDER 		= my_bone name:"SHOULDER" 		size:5  pos:[-19.39,-0.013,176.084]  wirecolor:red 
 	local ELBOW 		= my_bone name:"ELBOW" 			size:5  pos:[-47.155,2.628,163.611]  wirecolor:red
 	local FORARM 		= my_bone name:"FORARM" 		size:5  pos:[-72.66,-0.699,152.156]  wirecolor:red
 	local HAND 			= my_bone name:"HAND" 			size:5  pos:[-78.037,-1.401,149.741] wirecolor:red
  	
	local skeleton_data = #(PRE_SHOULDER, SHOULDER, ELBOW, FORARM, HAND)
	local skeleton_bones
	
	on createButtonPoint pressed do undo "Make Skeleton Points" on
	(
		delete helpers
		
		POS_BODY = for b in skeleton_data collect (point name:(tolower b.name + "_point") pos:b.pos wirecolor:b.wirecolor size:b.size box:on cross:on axistripod:on) 
	)
	
	fn bonetest data = if iskindof data array do
	(
		skeleton_bones = for k=1 to data.count-1 collect 
		(
			bone = boneSys.createBone data[k].pos data[k+1].pos [1,0,0]
			bone.name = "b_" + data[k].name
			bone
		)
	)
	on createButtonBones pressed do undo "Make Skeleton Bones" on 
	(
		delete geometry 
		
		bonetest skeleton_data
	)
	on createButtonBones2 pressed do undo "Make Skeleton Bones" on 
	(
		delete geometry 

		bonetest POS_BODY
	)
	
	on rol open do
	(
	)
)
createdialog rol

Here’s a template for how you could organize a task like this.

But, of course, there is still a lot of work to be done to get the bone chain right.

Hey,

In this part of the code:

boneA = boneSys.createBone bar[i].pos bar[(i)+1].pos [1,0,0]

you try to get the second position from the next item in the array, but at the end of the loop, there’s nothing there so you get the “pos is undefined” error. A way to fix this is to change the loop from
i = 1 to bar.count to i = 1 to bar.count -1

thanks Denis , I’m going to use this template as an example and improve my organization.
I am aware that I still have a long way to go.
but it’s complicated , I find that the 3ds max documentation is not very clear.

and yes Shopeca thank you ! I finally realized it last night too, it’s so stupid that you don’t think about it haha.
and I confess I’m still having trouble deciphering max’s error messages.