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

My NPC attacking script is not woking and is not giving any console output Help!?

Asked by 3 years ago

Hello i am making a npc that if you get near to it will start following you and start attacking when he gets too close but the npc doesn't move so can any one tell me what is wrong with this script? btw it doesn't say anything in the output

game.Loaded:Wait()

local Torso = script.Parent.Torso
local Target = nil
local LockWhileTrueLoop = false
local Animator = script.Parent.Humanoid.Animator
local Animations = {
    ["Attack"] = Animator:LoadAnimation(script.Attack),
    ["Walk"] = Animator:LoadAnimation(script.Walk)
}

function Patrol()
    if #game.Players:GetChildren() > 0 then
        for i,v in pairs(game.Players:GetChildren()) do
            if ((v.Character.Torso.Position - Torso.Position).Magnitude < 50) then
                LockWhileTrueLoop = true
                Target = v.Character
                LockWhileTrueLoop = false
            end
            wait(0.1)   
        end     
    end
end

function FollowPlayer()
    repeat
        if not ((Target.Torso.Position - Torso.Position).Magnitude < 4) then
            LockWhileTrueLoop = true
            Animations["Walk"]:Play()
            script.Parent.Humanoid:MoveTo(Target.Torso.Position)
            script.Parent.Humanoid.MoveToFinished:Wait()
            Animations["Walk"]:Stop()
            LockWhileTrueLoop = false
        else
            LockWhileTrueLoop = true
            Animations["Attack"]:Play()
            wait(0.06)
            if ((Target.Torso.Position - Torso.Position).Magnitude < 4) then
                Target.Humanoid:TakeDamage(3)
            end
            LockWhileTrueLoop = false
        end 
    until Target.Humanoid.Health == 0 or ((Target.Torso.Position - Torso.Position).Magnitude > 100)
end

while true do
    if Target == nil then
        Patrol()
    else
        FollowPlayer()
    end
    wait(0.1)   
end

this is all inside a normal script in the npc

1 answer

Log in to vote
1
Answered by 3 years ago

The only use of waiting for the game to load would be when you’re working with a LocalScript inside ReplicatedFirst. Try removing that line, if it doesn't work let me know.

1
There were still some bugs but i fixed them datboyPiter 52 — 3y
0
Nice NotFrindow 346 — 3y
Ad

Answer this question