Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make an AI Bot stay behind the player?

Hello, I have this script which makes an NPC Bot follow a player. I attempted to make the bot always stay behind the player at a good distance but I had an issue if the player is facing a different direction? How can I make it always go behind the Player no matter the way they are facing.

while FollowMaster == true do
	if (game.Players.CrazyCorrs.Character.HumanoidRootPart.Position-script.Parent.HumanoidRootPart.Position).Magnitude > 5 then
		script.Parent.Humanoid:MoveTo(game.Players.CrazyCorrs.Character.HumanoidRootPart.Position-Vector3.new(0,0,-2.3))
	end
	wait()
end

1 answer

The script is cut but anyway I would've instead used CFrames, just do this instead:

local howFar = CFrame.new(0,0,-2.3)
local humanoidRP = game.Players.CrazyCorrs.Character.HumanoidRootPart
script.Parent.Humanoid:MoveTo((humanoidRP.CFrame*howFar).position) -- now it will be more local than global (more related to the character than everything else). don't forget to do the ".position" because the result is a CFrame (the multiplication of a cframe and a cframe = another cframe) and MoveTo only accepts Vector3.

3 comments

0

Thanks

0

So glad I was able to help you. I forgot about mentioning an article if you wanna learn more about CFrames so here you go: https://developer.roblox.com/en-us/articles/Understanding-CFrame

0

And if your question was answered please mark it as so :D

This discussion is preserved as it was. New answers, comments, and votes are closed.