Need help with AI movement

Hey, I’ve tried to make a random AI movement NPC that stays within the base part. It works on square parts but not on triangles or other shapes. How could I do that ?

local char = script.Parent
local rootPart = char:WaitForChild("HumanoidRootPart")
local humanoid = char:WaitForChild("Humanoid")
local base = workspace.Enclos

local step = 2
local walkTime = math.random(3, 5)
local waitTime = math.random(1, 3)
local walkSpeed = math.random(5, 16)
local dis = 1 -- distance minimum des bords
local div = 2

humanoid.WalkSpeed = walkSpeed

while wait(step) do
	humanoid:MoveTo(
		(base.CFrame * 
			CFrame.new(
				math.random(-base.Size.x/div+dis,base.Size.x/div-dis),
				humanoid.HipHeight,
				math.random(-base.Size.z/div+dis,base.Size.z/div-dis))).p
	)
	wait(walkTime)
	humanoid.WalkSpeed = 0
	wait(waitTime)
	humanoid.WalkSpeed = walkSpeed
end
1 Like

“It works on square parts but not on triangles or other shapes. How could I do that ?”

So like a NPC moving part to part? Square only? I think i can do that

I made something for you, I don’t know if this is going to solve your problem.

I made a AI thing so the character moves to one block to another. i hope this helps

image

make any parts 1, 2, 3 don’t matter. name it destination, destination2, how much you even need then insert a script in the character and type

local OozyMage = script.Parent
local humanoid = OozyMage.Humanoid

while true do
humanoid:MoveTo(workspace.destination.Position)
humanoid.MoveToFinished:Wait()
humanoid:MoveTo(workspace.destination2.Position)
humanoid.MoveToFinished:Wait()
wait(34324213)

end

if you want it to go to the part then back to the part do

local OozyMage = script.Parent
local humanoid = OozyMage.Humanoid

while true do
humanoid:MoveTo(workspace.destination.Position)
humanoid.MoveToFinished:Wait()
humanoid:MoveTo(workspace.destination2.Position)
humanoid.MoveToFinished:Wait()

end

Man, I hope this helped you or anyone else.