Ipairs does not work in my array

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I am trying to make a script which does animations by getting the poses in a keyframe and then setting the Motor6D transform of the player.

  2. What is the issue? Include screenshots / videos if possible!
    I store my poses inside of an array but when I use ipairs, it does not loop in order.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve looked through some developer forum posts but I haven’t found a solution yet.

This is the KeyframeSequence structure below.
Screenshot_5

I have put my code below.

local Equip = {
	{
		LeftUpperArm = { Equip.Keyframe1.HumanoidRootPart.LowerTorso.UpperTorso.LeftUpperArm, "LeftShoulder" },
		LeftLowerArm = { Equip.Keyframe1.HumanoidRootPart.LowerTorso.UpperTorso.LeftUpperArm.LeftLowerArm, "LeftElbow" },
		RightUpperArm = { Equip.Keyframe1.HumanoidRootPart.LowerTorso.UpperTorso.RightUpperArm, "RightShoulder" },
		RightLowerArm = { Equip.Keyframe1.HumanoidRootPart.LowerTorso.UpperTorso.RightUpperArm.RightLowerArm, "RightElbow" }
	},
	{
		RightUpperArm = { Equip.Keyframe2.HumanoidRootPart.LowerTorso.UpperTorso.RightUpperArm, "RightShoulder" },
		RightLowerArm = { Equip.Keyframe2.HumanoidRootPart.LowerTorso.UpperTorso.RightUpperArm.RightLowerArm, "RightElbow" }
	},
	{
		LeftUpperArm = { Equip.Keyframe3.HumanoidRootPart.LowerTorso.UpperTorso.LeftUpperArm, "LeftShoulder" },
		LeftLowerArm = { Equip.Keyframe3.HumanoidRootPart.LowerTorso.UpperTorso.LeftUpperArm.LeftLowerArm, "LeftElbow" },
		RightUpperArm = { Equip.Keyframe3.HumanoidRootPart.LowerTorso.UpperTorso.RightUpperArm, "RightShoulder" },
		RightLowerArm = { Equip.Keyframe3.HumanoidRootPart.LowerTorso.UpperTorso.RightUpperArm.RightLowerArm, "RightElbow" }
	}
}

script.Parent.TextButton.Activated:Connect(function()
	RunService.Stepped:Connect(function()
		for i, v in ipairs(Equip) do
			print(i)
			print(v)
			for i, v in pairs(v) do
				for n = 0, 1 do
					Player.Character[i]:FindFirstChild(v[2]).Transform = Player.Character[i]:FindFirstChild(v[2]).Transform:Lerp(v[1].CFrame, n)
				end
			end
			wait(0.5)
		end
	end)
end)

Edit: I have also put what I get in my output below.

Every time someone clicks the button, you connect to RunService.Stepped. That means that however many times you click the button is the number of times each frame that your loop will be running. Since your loop waits 0.5 seconds between iterations, the new loop that starts each frame will start before the current loop’s next iteration; one frame is less time than 0.5 seconds.

1 Like

Ah right. This only problem I have with it is that is I don’t know how else I would make the animated parts move before the next frame. I can’t get rid of RunService.Stepped otherwise it won’t work at all. Is there like a yielding version of wait() that I could use?

wait does yield for the loop it’s in. I don’t know what you’re expecting Stepped to do here, but it does not wait for any of the currently yielding iterations; it just starts a new loop.

1 Like

Interesting. I put Stepped there so that the default roblox animations wouldn’t just override the animation I want, I just tested having only the for loop and it does nothing. This wouldn’t be a problem if I only wanted a static animation, but since I have more than 1 keyframe at different times, it’s not particularly convenient and I need a way to do it.