How to make an NPC go left or right relative to their orientation?

I want to make an NPC circle a player by having them constantly look towards the player and go either left or right, like this:
image

I’ve got them constantly looking at the player but if I try to move them they go in that direction regardless of their orientation:


How can I fix this?

Code(Script inside Actor inside the NPC):

local function handleCombat()
	
	NPC:SetAttribute("State", "Circulating")
	
	local player = NPC:GetAttribute("Player")
	local playerHRP
	if player == "" then
		warn("No player engaged. Disconnecting combat...")
		NPC:SetAttribute("InCombat", false)
	else
		local check = game.Workspace:FindFirstChild(player)
		if check then
			playerHRP = check.HumanoidRootPart
			local npcHRP = NPC:FindFirstChild("HumanoidRootPart")
			if npcHRP then
				npcHRP.CFrame = CFrame.lookAt(npcHRP.Position, playerHRP.Position)
			end
		else
			warn("Player:", player, "could not be found. Disconnecting combat...")
			NPC:SetAttribute("InCombat", false)
		end
	end

RunService.Heartbeat:ConnectParallel(function()
	wait()
	if NPC:GetAttribute("InCombat") == true then
			if combatConnection == nil then
				combatConnection = RunService.Heartbeat:Connect(handleCombat)
			end
		end
	if NPC:GetAttribute("State") == "Circulating" then
		humanoid:MoveTo(humanoidRootPart.Position + Vector3.new(1, 0, 0)) -- humanoidRootPart is the NPC's HRP
		task.wait(5)
    end
end)

I should mention that this is also the first time I’m using Parallel Lua.

You gotta use cframe

ex.

local destination = (hrp.CFrame * CFrame.new(15,0,0)).Position
hum:MoveTo(destination)
2 Likes

Thanks a lot! It’s working!
–some more words so I can post this

1 Like