AlignOrientation not working on Client side

I have an NPC system that used AlignOrientations to make NPCs face the nearest player. This works perfectly in server side however it becomes very unperformant. So i moved it to the client.

However, this randomly stops working when transported to the client. Even when the AlignOrientation is instanced locally and the NetworkOwnership is set to the player.

Heres it NOT working

local function EnemyFunc(v)

	local humanoid = v:WaitForChild("Humanoid")
	local detectEvent = v:FindFirstChild("Detect")
	local animator = humanoid:WaitForChild("Animator")

	local lastHealthValue = tonumber(humanoid.Health)
	local invulnerable = false


	task.defer(function()
		local diff
		local angle
		local humanoidRootPart = v:FindFirstChild("HumanoidRootPart")
		local AlignOrientation, attachment
		local closestPlayer, distance
		v.Humanoid.AutoRotate = false
		
		if humanoidRootPart then
			AlignOrientation = Instance.new("AlignOrientation")
			AlignOrientation.Mode = Enum.OrientationAlignmentMode.OneAttachment
			AlignOrientation.Responsiveness = 200
			attachment = Instance.new("Attachment")
			attachment.Parent = humanoidRootPart
			attachment.Position = Vector3.new(0,0,0)
			AlignOrientation.Attachment0 = attachment
		end
		
		runService.RenderStepped:Connect(function()
			closestPlayer, distance = getClosestPlayer(v)
			if closestPlayer and distance and AlignOrientation then
				diff = v.HumanoidRootPart.Position - closestPlayer.Character.PrimaryPart.Position

				angle = math.atan2(diff.X, diff.Z)
				AlignOrientation.CFrame = CFrame.new(0, 212, 0)
			end
		end)
	end)
end

How would i fix this? thank

Have you found a solution to this? I’m running into the same problem.

Sorry for the late reply, unfortunately not and i had to shelf my project because of it.

You can use CFrame.lookAt() and update it using RenderStepped.

Cant as editing the CFrame property would stutter and ruin the pathfinding

1 Like

The issue you faced has nothing to do with AlignOrientation or the code in general. Its simply just how Roblox works.

For some reason, local functions seem to break sometimes when run on a client.

A way to fix this would simply be to instead of a local function, run the code through a remote event from the server. Its not the preferred method, but it works well enough to fix the problem.

Good to know, thanks. Il try that sometime soon and will update if it fixes the issue.

You forgot to set MaxTorque, and you don’t need to set attachments new position.
Take a look at how I used AlignOrientation on client: How to make the player fly while a value is true - #9 by ChiDj123


Are we gonna ignore the fact that you are not even updating your AlignOrientation CFrame

That’s the stupidest shi I’ve heard in my entire life, you just basically said code locally decides to not work on it’s own, that makes no sense buddy

1 Like

Yeah. I’d take this advice instead.
Not sure what the other person was on about haha.
:+1:

Your AlignOrientation is never parented, so it doesn’t actually work. Also, you’re calculating diff and angle but then setting a static CFrame that doesn’t use them. local function EnemyFunc(v)
local humanoid = v:WaitForChild(“Humanoid”)
local humanoidRootPart = v:WaitForChild(“HumanoidRootPart”)

humanoid.AutoRotate = false
humanoidRootPart:SetNetworkOwner(game.Players.LocalPlayer)

local AlignOrientation = Instance.new("AlignOrientation")
AlignOrientation.Mode = Enum.OrientationAlignmentMode.OneAttachment
AlignOrientation.Responsiveness = 200
AlignOrientation.MaxTorque = 10000
AlignOrientation.Parent = humanoidRootPart

local attachment = Instance.new("Attachment")
attachment.Parent = humanoidRootPart
AlignOrientation.Attachment0 = attachment

local conn = runService.RenderStepped:Connect(function()
	local closestPlayer, distance = getClosestPlayer(v)
	if closestPlayer and closestPlayer.Character and closestPlayer.Character.PrimaryPart then
		local targetPos = closestPlayer.Character.PrimaryPart.Position
		AlignOrientation.CFrame = CFrame.new(humanoidRootPart.Position, targetPos)
	end
end)

humanoid.Died:Connect(function()
	conn:Disconnect()
	AlignOrientation:Destroy()
	attachment:Destroy()
end)

end

Well im sorry to burst your bubble buddy, but the fact that that happens is true even if you dont believe it. It doesnt change anything. It may be stupid, yes, but it doesnt make any less true.

Theres also the fact that I just stated a way to fix the problem, and instead of being grateful, you decide to call me an idiot. This bug is a known problem that has been happening for years.

code cannot simply not work in unintended behavior, stop spreading ur schizophrenic lies buddy. you simply have huge skill issue, accept that

Thanks for replying frend but me thinks theres no need for that kinda talk here. As for everyone else contributing. Thank you, I will read these later as im still working on a commission, il try out all of these once i have the time and hopefully I am able to fix this problem.

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