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
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.
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
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)
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.
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.