Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 9 years ago

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

Log in to vote
0
Answered by 9 years ago

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.

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. ZeptixBlade 215 — 9y
0
Odd. Maybe property is returning a Vector3 value instead of a string. I'll look into it. aquathorn321 858 — 9y
0
Nope. Not sure what's up with it. aquathorn321 858 — 9y
Ad

Answer this question