NPC Animations And Random Direction Walking Not Working

Hey There!
So i was making an kind of “enemy” NPC that i want to walk away when the player gets closer and closer so basically get close to a certain distance.

And i cant quiet figure out how to achieve this.
I made the basics but not even the ANIMATE_FIGURE() function seems to properly work

NOTICE
THIS IS NOT A REGULAR DUMMY IT IS A CHEST THEREFORE IT CANT USE
THE FUNCTIONS OF A NORMAL DUMMY NPC SCRIPT!!

THE WALKING AND RUNNING ANIMATIONS WORK BECOUSE I USE A CUSTOM RIG
SO I THOUGHT TO USE THE HUMANOIDROOT PARTS CFRAME TO MOVE IT FORWARD, LEFT, RIGHT ETC…

image

local STATE = 0

local FIGURE = script.Parent
local HUMANOID_ROOT_PART = FIGURE.HumanoidRootPart
local HUMANOID = FIGURE.Humanoid

local IDLE_ANIMATION = require(game:GetService('ReplicatedStorage').ANIMATIONS).CHEST_ANIMATIONS.IDLE
local WALKING_ANIMATION = require(game:GetService('ReplicatedStorage').ANIMATIONS).CHEST_ANIMATIONS.WALKING

local function LOAD_ANIMATION(ANIMATION_ID)
	local ANIMATION = Instance.new("Animation")
	ANIMATION.AnimationId = ANIMATION_ID
	HUMANOID:LoadAnimation(ANIMATION):Play()
end

local function IDLE()
	STATE = 1
	LOAD_ANIMATION(IDLE_ANIMATION)
end

local function WALK()
	STATE = 2
	LOAD_ANIMATION(WALKING_ANIMATION)
end

local function CALCULATE_POSITION()
	local CURRENT_CFRAME = HUMANOID_ROOT_PART.CFrame
	local NEW_CFRAME = CURRENT_CFRAME * CFrame.new(Vector3.new(math.random(), 0, 0))
	return NEW_CFRAME.Position
end

local function ANIMATE_FIGURE()
	WALK()
	while wait(0.1) do
		if STATE == 2 then
			local NEW_POSITION = CALCULATE_POSITION()
			HUMANOID_ROOT_PART.CFrame = CFrame.new(NEW_POSITION)

			wait(math.random(1, 15))

			IDLE()

			wait(math.random(1, 10))

			WALK()
		end
	end
end

ANIMATE_FIGURE()
  1. Get the move direction the NPC has to walk to to go towards the player.
  2. Make it negative (do it times -1)

Also this is not how you’re supposed to move an NPC, read the humanoid docs for further insight on this.

You can if you rig it up properly. If you’d like to test if you can walk with it, then put it inside StarterPlayer and call the model StarterCharacter then mess with settings such as HipHeight and set quite a few parts such as the legs of the model to CanCollide = false

To get the direction the player is at just use

CFrame.new(chestPosition, characterPosition).lookVector

You can also just use

(chestPosition - characterPosition).Unit

I will need to look into this a little more when i get back home, But thanks this helped alot.