Making a part face away from the player

So I’ve been recently doing documentation on CFrames, and wanted to challenge myself by making an AI that will face a random direction when there is no player nearby, but will face away from the player if a player is nearby and flee. But for some reason, It’ll constantly face one direction and never update and not face the right position. I’ve at it for nearly 2 hours now trying to figure this out and just can’t for the life of me. If anyone is able to show me my errors it’d be greatly appreciated!

(Note: Core is the PrimaryPart of the model)
Function:

-- Faces a random Direction
local function FaceRandomDir(ClosestPlr)
	if not ClosestPlr then
		local RandomDir = math.random(MinDir,MaxDir)

		Core.CFrame *= CFrame.Angles(0,RandomDir,0) --Makes the part face away from player
	else
		local Char = ClosestPlr.Character
		local Dir = (Core.Position - Char.HumanoidRootPart.Position).Y
		local FaceAt = CFrame.lookAt(Core.Position, Core.Position + Vector3.new(0,(-Dir),0))
		
		Core.CFrame = FaceAt --Makes the part face away from player
	end
end

Examples of my issues:

Where is the rest of your code that calls the function?

Also just to clean up your code but you should use CFrame.lookAlong instead:

-- Faces a random Direction
local function FaceRandomDir(ClosestPlr)
	if not ClosestPlr then
		local RandomDir = math.random(MinDir,MaxDir)

		Core.CFrame *= CFrame.Angles(0, RandomDir, 0) --Makes the part face away from player
	else
		local Char = ClosestPlr.Character
		local FaceAt = CFrame.lookAlong(Core.Position, Char.HumanoidRootPart.CFrame.LookVector)

		Core.CFrame = FaceAt --Makes the part face away from player
	end
end
1 Like

I’m assumming that MinDir, MaxDir are your angles that are not in radians, since CFrame.Angles works with radians and not degrees you should convert RandomDir to radians with math.rad(RandomDir), otherwise you would get some big rotations that don’t make sense

Also if those variables are in radians, math.random would just return a whole number since most of the time radians have decimals, so it would be better to have them in degrees for the random angle generation and then converting that angle into radians

1 Like

The function that calls it was a while true loop that waits every second, finds the nearest player (if in range) and gives it to the FaceRandomDir function. Then thats pretty much the rest of the code as of now. They don’t move yet but only face a certain direction varying on if a player if is nearby or not.

Ill try out the code tomorrow as I’m unavailable for the rest of the day

Yeah , both variables were degrees, i’ll make sure to convert them into radians when i go to edit my code again!!

Pretty much looked like this. Sorry for the weird formatting, I typed it from memory on mobile.


-- Faces a random Direction
local function FaceRandomDir(ClosestPlr)
	if not ClosestPlr then
		local RandomDir = math.random(MinDir,MaxDir)

		Core.CFrame *= CFrame.Angles(0,RandomDir,0) --Makes the part face away from player
	else
		local Char = ClosestPlr.Character
		local Dir = (Core.Position - Char.HumanoidRootPart.Position).Y
		local FaceAt = CFrame.lookAt(Core.Position, Core.Position + Vector3.new(0,(-Dir),0))
		
		Core.CFrame = FaceAt --Makes the part face away from player
	end
end


local function StartUp()
 task.spawn(function()
  while true do
   task.wait(1)

    ClosestPlr, Dis = GetClosestPlayer()
    FaceRandomDir(ClosestPlr)
   end
  end
end

StartUp()

It worked! Thanks again for the help, I’ll look more into how to utilize CFrame.LookAlong and CFrame.Angles

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.