#Npc head follow player R6
1 messages · Page 1 of 1 (latest)
for some reason
my script doesnt work
local char = script.Parent
local head = char:WaitForChild("Torso")
local hrp = char:WaitForChild("HumanoidRootPart")
local neck = head:WaitForChild("Neck")
local xOffSet = neck.C0.X
local yOffSet = neck.C0.Y
local zOffSet = neck.C0.Z
local function getClosestPlayerHrp(dist)
local closestHrp = nil
for i, v in pairs(game.Players:GetChildren()) do
local tmpChar = v.Character or v.CharacterAdded:Wait()
local tmpHrp = tmpChar:FindFirstChild("HumanoidRootPart")
local tmpDist = (tmpHrp.Position - hrp.Position).Magnitude
if tmpHrp and tmpDist < dist then
closestHrp = tmpHrp
dist = tmpDist
end
end
return closestHrp, dist
end
while wait() do
local closestHrp, dist = getClosestPlayerHrp(25)
if closestHrp then
local dir = (closestHrp.Position - hrp.Position).Unit
local vecA = Vector2.new(hrp.CFrame.LookVector.X, hrp.CFrame.LookVector.Z)
local vecB = Vector2.new(dir.X, dir.Z)
local dotValue = vecA:Dot(vecB)
local crossValue = vecA:Cross(vecB)
local ht = hrp.Position.Y - closestHrp.Position.Y
local upAngle = math.atan(ht/dist)
local angle = math.atan2(crossValue, dotValue)
if angle > math.pi/3 then
angle = math.pi/3
elseif angle < -math.pi/3 then
angle = -math.pi/3
end
neck.C0 = CFrame.new(neck.C0.Position) * CFrame.Angles(-upAngle, -angle, 0) * CFrame.Angles(math.rad(-90), 0, math.rad(-180))
end
end
i put it on r6 rig head
but still it doesnt follow me
the whole model itself?
ye like the rig
ok
wait
do you just want the head to follow?
OH
you want the head to turn towards the player i see
mb
yea
ok put the script in the rig
i did
and this works btw even though this was made by ai i put it in a baseplate and it works "local char = script.Parent
local torso = char:WaitForChild("Torso") -- not head
local head = char:WaitForChild("Head") -- if needed
local hrp = char:WaitForChild("HumanoidRootPart")
local neck = torso:WaitForChild("Neck")
local xOffset = neck.C0.X
local yOffset = neck.C0.Y
local zOffset = neck.C0.Z
local function getClosestPlayerHrp(dist)
local closestHrp = nil
local Players = game:GetService("Players")
for _, player in pairs(Players:GetPlayers()) do
local tmpChar = player.Character
if tmpChar and tmpChar:FindFirstChild("HumanoidRootPart") then
local tmpHrp = tmpChar.HumanoidRootPart
local tmpDist = (tmpHrp.Position - hrp.Position).Magnitude
if tmpDist < dist then
closestHrp = tmpHrp
dist = tmpDist
end
end
end
return closestHrp, dist
end
while task.wait() do
local closestHrp, dist = getClosestPlayerHrp(25)
if closestHrp then
local dir = (closestHrp.Position - hrp.Position).Unit
local vecA = Vector2.new(hrp.CFrame.LookVector.X, hrp.CFrame.LookVector.Z)
local vecB = Vector2.new(dir.X, dir.Z)
local dotValue = vecA:Dot(vecB)
local crossValue = vecA:Cross(vecB)
local heightDiff = hrp.Position.Y - closestHrp.Position.Y
local upAngle = math.atan(heightDiff / dist)
local angle = math.atan2(crossValue, dotValue)
angle = math.clamp(angle, -math.pi / 3, math.pi / 3)
local baseC0 = CFrame.new(xOffset, yOffset, zOffset)
local rotation = CFrame.Angles(-upAngle, -angle, 0) * CFrame.Angles(math.rad(-90), 0, math.rad(-180))
neck.C0 = baseC0 * rotation
end
end
"
its ai but dont hate because i got it to work alr?
alr