I’m trying to make a bot for my game, when the player walks too close to the bot I want the bot to stop. I’ve tried moving the humanoid to its humanoidRootPart, it just starts spinning around endlessly. How could I do this? This is my script:
module.playerFound = function(botCharacter) -- this runs when the player is too close to the bot
local humanoid = botCharacter:FindFirstChildOfClass("Humanoid")
local humanoidRootPart = botCharacter:FindFirstChild("HumanoidRootPart")
if humanoid and humanoidRootPart then
print("Move")
humanoid:MoveTo(humanoidRootPart.Position) -- this is it moving to its self
humanoid.WalkToPart = humanoidRootPart
humanoid.WalkToPoint = humanoidRootPart.Position
humanoid.MoveToFinished:Wait()
print("Moved") -- both of these print right after each other
end
return
end
while wait() do
if (botCharacter.Position - player.Character.Humanoid.Position).magnitude >= 2 then -- Change the "2" accordingly
botCharacter.Humanoid.WalkSpeed = 0
end
Are you sure this doesn’t work? I used it on my NPCs too and it works fine. Also there’s no need to set all the properties (WalkToPart and WalkToPoint), MoveTo() will be enough.
Aha, I think I know why it isn’t working. It works (kinda) if I do this:
while wait() do
humanoid:MoveTo(botCharacter.PrimaryPart.Position)
humanoid.MoveToFinished:Wait()
end
Heres the script that actually tells the player to move to a part.
local waypoints = path:GetWaypoints()
for index, waypoint in pairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
I think this is interfering with me trying to walk it to the humanoidRootPart, because it goes through the waypoints and so when I try to Humanoid:MoveTo(humanoidRootPart.Position) it doesnt work because it just goes back to the next waypoint. How would I fix this tho?? @AndroidBoyz