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

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

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

✓ Accepted answer

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.

2 comments

1

There were still some bugs but i fixed them

0

Nice

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