Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

When a player gets close to a NPC Have it's head follow them?

I was saying, that when you get close to the npc, it looks at you, then when you step away it just resets to the way it was before you got close to it?

2 comments

0

You have put no input into this, we don't just blatantly give you scripts. You have to try yourself and research more!

0

How do I add this to the model?

2 answers

Constantly check a region3 or check every character. If a char is inside a radius then continuously cframe the head facing the char until the char leaves or the rotation is too great. This is a vague example for a script inside an anchored npc model. I hope you can use the math at least. Most importantly, you can face a different position by doing CFrame.new(myPos,otherPos).

local radius=20
local npc = script.Parent
local head = npc:WaitForChild('Head')

local function rotationDifference(oldCF,newCF)
	--self explanatory
	return (newCF.lookVector - oldCF.lookVector).magnitude
end

while wait(1) do
	--loop through characters
	local cf = head.CFrame
	local p = cf.p
	
	for _,player in pairs(game.Players:GetPlayers()) do
		local char = player.Character
		if char then
			local ppart = char.PrimaryPart
			if ppart then
				
				local pos = ppart.Position
				local isInRadius = (pos-p).magnitude < radius
				if isInRadius then
					--found close character
					if rotationDifference(cf , CFrame.new(p,pos)) < 1.25 then
						--character is in front
						
						--look at character until it leaves or goes behind
						repeat
							p = head.Position
							pos = ppart.Position
							head.CFrame = CFrame.new(p,pos)
							wait(1/20)
						until ( (pos-p).magnitude > radius ) or ( rotationDifference(cf , head.CFrame ) > 1.25 )
						head.CFrame = (cf-cf.p)+p --old rotation
						break
					end
				end
			end
		end
	end
end

Okay. I understand what you mean by your question. First thing, have you even tried researching it? It doesn't seem like you looked for the answer.

Here, I have a few scripts that may be able to assist you. You would put the following scripts inside the NPC model.

wait(1)
follow = false
while true do
if game.Players.Player1.Character.Torso ~= nil then
	if game.Players.Player1.Character.Torso.Position.X - game.Workspace.Soldier.Torso.Position.X <= 10 then
		follow = true
	end
end	

if follow then
	game.Workspace.Soldier.Humanoid.WalkToPoint = game.Players.Player1.Character.Position
end
 wait(.5)
end

These use a Following Soldier NPC I made. I suppose you know how to do the rest.

local soldier = script.Parent
soldier.GunStorage.Gun.Parent = soldier

for _, script in pairs(soldier.ModuleScripts:GetChildren()) do
	if not game.ServerStorage:FindFirstChild(script.Name) then
		script:Clone().Parent = game.ServerStorage
	end
end

wait(1)


local AI = require(game.ServerStorage.ROBLOX_SoldierAI).new(soldier)


local DestroyService = require(game.ServerStorage.ROBLOX_DestroyService)

local function clearParts(parent)
	for _, part in pairs(parent:GetChildren()) do
		clearParts(part)
	end
	local delay
	if parent:IsA("Part") then
		delay = math.random(5,10)
	else
		delay = 11
	end
	DestroyService:AddItem(parent, delay)
end

soldier.Humanoid.Died:connect(function()
	AI.Stop()
	math.randomseed(tick())
	clearParts(soldier)
	script.Disabled = true
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.