How could I make this MoveTo script less messy?

Ahh, finally! After about 6 hours of reading I am finally a New Member on the Roblox Devfourm! Let me get on with it now.
Hello! I have this already working bit of code that moves Freddy (an NPC) to Parts in a Folder. Each parts name starts with pos, and I just put numbers adding up at the end of each one. Here is my script:

Freddy = game.Workspace.NPCs.Freddy.Humanoid
fred1 = game.Workspace.FreddyPositions.pos1.Position
fred2 = game.Workspace.FreddyPositions.pos2.Position
fred3 = game.Workspace.FreddyPositions.pos3.Position
fred4 = game.Workspace.FreddyPositions.pos4.Position
fred5 = game.Workspace.FreddyPositions.pos5.Position
fred6 = game.Workspace.FreddyPositions.pos6.Position
fred7 = game.Workspace.FreddyPositions.pos7.Position
fred8 = game.Workspace.FreddyPositions.pos8.Position

Freddy:MoveTo(fred1)
Freddy.MoveToFinished:wait()
Freddy:MoveTo(fred2)
-- on and on and on

(This is my first time putting in a code block so I hope I did it correctly)
This is only a small portion of the full script, but it shows what iā€™m concerned with. All of the variables at the beginning of the script look very messy and I think there is a better way of doing this. I am going to be needing more than just 8 positions so I would like to fix this messiness as soon as possible. Since this is my first ever post here, please tell me if I did something wrong! Thanks!

2 Likes

I suggest something that loops through each position like this:

Freddy = workspace.NPCs.Freddy.Humanoid -- it's much simpler to use workspace rather than game.Workspace

while wait() do -- remove if you don't want it to repeat
 for i = 1, 8 do -- for positions 1 through 8
  Freddy:MoveTo(workspace.FreddyPositions["pos" .. i].Position)  -- move to member "posX" where x is the number
  Freddy.MoveFinished:Wait() -- i believe :wait() is depricated, use :Wait() instead
 end
end -- remove this too if you don't want it to repeat

Hope this helps!

ā€“ LuaConstructor

1 Like

@TheCarbyneUniverse @LuaConstructor Thank you both for the help! My question has been answered.

2 Likes