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

how to detect distance, make NPC face player?

Asked by
ere376 0
9 years ago

i need help, right now i have a few NPC's running around with scripts for their ai and i usually like to do all the scripting myself and learn from other players scripts, but i can not find a script close to what im looking for, i need the script to detect if a players torso is within say 10 blocks of the npc's torso and have him look at it heres my script soo far, any help?

torso = script.Parent:findFirstChild("Torso")
ai1 = script.Parent.AI.Disabled
ai2 = script.Parent.AI2.Disabled

for i, plr in ipairs(game.Players:GetChildren()) do
    --NEED WAY TO SENSE plr's TORSO DISTANCE FROM AI TORSO
    if ai2 == false then
        if plr:GetRankInGroup(1129497) >= 250 then
        ai2 = true
        --NEED WAY TO MAKE MODEL LOOK AT PLAYER
        end
    end
    if ai1 == false then
        if plr:GetRankInGroup(1129497) >= 250 then
        ai1 = true
        --NEED WAY TO MAKE MODEL LOOK AT PLAYER
        end
    end
end

ps. i want it when a High rank in my group walks by they look at him hense why theres a rank sensor

1 answer

Log in to vote
1
Answered by
LevelKap 114
9 years ago

Here's some code that detects the NEAREST player. You should be able to adapt this into what you need. Hope this helps!

local player = game.Players.LocalPlayer

function FindClosestPlayer()
    local minDist = 999999
    local closest = nil
    for _,v in pairs(game.Players:GetPlayers()) do
        local dist = player:DistanceFromCharacter(v.Character.Torso.Position)
        if v ~= player and dist < minDist then
            minDist = dist
            closest = v.Character
        end
    end
    return closest
end

print(FindClosestPlayer())


0
Thanks yeah i can adapt this ere376 0 — 9y
Ad

Answer this question