How would i make this into ai moving left and right only, instead of random positions?

Hi. I want to achieve a basic ai script that moves only left to right. I found this in one of my old models. Here is the script:

> local character = script.Parent
> local humanoidRootPart = character:WaitForChild('HumanoidRootPart')
> local humanoid = character:WaitForChild('Humanoid')
> 
> local base = character.Parent.Parent.Base
> 
> local step = 2
> 
> while wait(step) do
> 	humanoid:Move(Vector3.new(math.random(base.Position.X), 0, math.random(base.Position.Z)))
> end

Using the HumanoidRootPart’s CFrame you can just Index the Right vector and generate Positions with that.

NPC_HumanoidRootPart.CFrame.RightVector

Vector3 CFrame.RightVector

Or you can remove the Z from the equation

humanoid:Move(Vector3.new(math.random(base.Position.X * -1 , base.Position.X), 0, 0,))
1 Like