#Help with an NPC

6 messages · Page 1 of 1 (latest)

safe summit
#

So i tried scripting a demon npc for a demon slayer game but the npc dosent move neither does he attack me could anyone look over my script and tell me if i did something wrong?

#

local demon = script.Parent
local humanoid = demon:WaitForChild("Humanoid")
local attackRange = 100

while true do
local closestPlayer = nil
local closestDistance = math.huge

for _, player in ipairs(game.Players:GetPlayers()) do
    if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
        local distance = (player.Character.HumanoidRootPart.Position - demon.HumanoidRootPart.Position).Magnitude
        if distance < closestDistance then
            closestPlayer = player
            closestDistance = distance
        end
    end
end

if closestPlayer and closestDistance <= attackRange then
    humanoid:MoveTo(closestPlayer.Character.HumanoidRootPart.Position)
    humanoid:MoveToFinished:Wait()

    -- Attack logic goes here
    -- You can use humanoid:TakeDamage() or any other attack mechanism

    wait(1) 
else
    humanoid:MoveTo(demon.HumanoidRootPart.Position) 
end

wait(0.1) 

end

#

If i dont respond directly ping me please

#

Oh hi there

#

xD

#

yea i know but i first wanted to get him move