Need Help with AI Character random Movement

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 would like to have my Models move have random movement while the game plays, for example, while players are in game and playing, there will be a horse eating grass and walking around a field and a chicken clucking and walking around a farm etc

  1. What is the issue? Include screenshots / videos if possible!

I have tried so many different methods to achieve this, I can get the model move from point A to B but I can’t get the animal to turn around. my latest script is this, I have used waypoints, the animal just randomly moves all over the place in all different directions and doesn’t even go to the way points. (My NPC, does the loop perfectly, just not the animals)

local Humanoid = script.Parent.Humanoid
local Folder = game.Workspace.Path – put your folder name here
local loop = true – rename it to false if you don’t want it to loop

if loop then
while true do
for i, v in pairs(Folder:GetChildren()) do
Humanoid:MoveTo(Folder:FindFirstChild(i).Position)
Humanoid.MoveToFinished:Wait()
end
end
else
for i, v in pairs(Folder:GetChildren()) do
Humanoid:MoveTo(Folder:FindFirstChild(i).Position)
Humanoid.MoveToFinished:Wait()
end
end

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Ive looked for solutions, ive tried to hire people on fiver and no one can get this right for me.

2 Likes

Try this code

local Humanoid = script.Parent.Humanoid
local Folder = game.Workspace.Path
local loop = true

if loop then
	while task.wait() do
		for _, Part in Folder:GetChildren() do
			Humanoid:MoveTo(Part.Position)
			Humanoid.MoveToFinished:Wait()
		end
	end
else
	for _, Part in Folder:GetChildren() do
		Humanoid:MoveTo(Part.Position)
		Humanoid.MoveToFinished:Wait()
	end
end
1 Like

just tried it now, its doing the same thing. Thanks for trying

1 Like

What exactly is the problem?

  1. The code doesn’t work.
  2. The code works, but it doesn’t work the way I need it to.

The code is working the animal is moving very jittering, it’s going backwards and forwards, then rotating randomly. it’s not a smooth movement. I have the waypoints set out in a square. My NPC is moving smoothly and perfectly with the same script.

If the animal movement looks glitchy it’s probably because of Network ownership, the player could be taking Network ownership and losing it (this does not cause the sharp turns though)

Just use math.random to move randomly in different directions.

local Humanoid = script.Parent.Humanoid
local studsToMove = 50
local loop = true

if loop then
	while task.wait() do
        local position = script.Parent.HumanoidRootPart.Position + Vector3.new(math.random(-studsToMove,studsToMove),0,math.random(-studsToMove,studsToMove))
		Humanoid:MoveTo(position)
		Humanoid.MoveToFinished:Wait()
	end
else
	local position = script.Parent.HumanoidRootPart.Position + Vector3.new(math.random(-studsToMove,studsToMove),0,math.random(-studsToMove,studsToMove))
	Humanoid:MoveTo(position)
	Humanoid.MoveToFinished:Wait()
end