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

What's an efficient way to make an NPC face your character?

I'm currently doing this, which works fine;

while true do
	wait()
	local plr = playerValue.Value
	if plr then
		local direction = (plr.Character.Torso.Position - bot.Torso.Position) * Vector3.new(1, 0, 1)
		bot.Torso.CFrame = CFrame.new(bot.Torso.Position,bot.Torso.Position + direction)
	end
end

with playerValue being an ObjectValue equal to the correct player.

However, with lots of NPCs this causes lots of lag. The Script Performance window shows that this is very inefficient, on a large scale at least.

Do you know any more efficient method of making the bot face your character?

1 answer

You could use the changed method:

playerValue.Value.Character.Torso.Changed:connect(function(property)
if string.lower(property) == "position" then
local direction = (playerValue.Value.Character.Torso.Position - bot.Torso.Position)
bot.Torso.CFrame = CFrame.new(bot.Torso.Position,bot.Torso.Position + direction)
end
end)

I'm not sure if this would be considered more efficient, but it wouldn't cause lag if the player isn't moving.

3 comments

0

For some reason my walking around doesn't trigger the Changed event, but if I manually set the Position property it does trigger it. This is weird.

0

Odd. Maybe property is returning a Vector3 value instead of a string. I'll look into it.

0

Nope. Not sure what's up with it.

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