Npc instantly being teleported into the void upon looking towards player

Whenever I rotate my npc with

local lookAtPos = Vector3.new(targetRoot.Position.X, root.Position.Y, targetRoot.Position.Z)
root.CFrame = CFrame.new(root.Position, lookAtPos)

it’s Y position gets changed to -340282346638528859811704183484516925440

the npc has an objectvalue called lasthit which is whoever attacked it last, if lasthit is changed by an attack via hit.Parent.Values.LastHit.Value = script.Parent.Owner.Value it works fine but if last is changed by a script within the npc this happens.

Here’s the script that changes lasthit causing the rotation script to teleport the npc into the void

local RunService = game:GetService("RunService")
local npc = script.Parent
local root = npc:WaitForChild("HumanoidRootPart")
local lastHitValue = npc.Values:WaitForChild("LastHit")

RunService.PostSimulation:Connect(function()
	for _, attack in pairs(workspace.Attacks:GetChildren()) do
		local attackPart = nil
		if attack:IsA("BasePart") and attack:FindFirstChild("ProjHP") then
			attackPart = attack
		elseif attack:IsA("Model") and attack:FindFirstChild("ProjHP") and attack.PrimaryPart then
			attackPart = attack.PrimaryPart
		end

		if attackPart and attack:FindFirstChild("Moving") and attack.Moving.Value == true then
			local distance = (root.Position - attackPart.Position).Magnitude
			if distance < 25 then
				npc.MoveR.Event:Fire()

				local owner = attack:FindFirstChild("Owner") and attack.Owner.Value
				if owner and owner:IsA("Model") then
					task.delay(1.2, function()
						if owner:IsDescendantOf(game)
							and owner:FindFirstChild("HumanoidRootPart")
							and owner:FindFirstChild("Humanoid")
							and not (owner:FindFirstChild("Dead") and owner.Dead.Value) then

							print("Assigning LastHit to:", owner.Name)
							lastHitValue.Value = owner
						else
							print("Skipped assigning LastHit — owner was invalid or dead.")
						end
					end)
				end
			end
		end
	end
end)
1 Like

Okay, I found the issue, for anyone with the same problem whenever the npc tries to face itself it gets sent to -340282346638528859811704183484516925440 meaning there is probably something in your code causing the npc to want to face itself.

1 Like

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