I'm making a monster and I noticed that he only attacks one player and no one else. Is there any reason for this and is there a solution?

wait(1)
local path = game:GetService("PathfindingService"):CreatePath()
local run = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.Parent.Animations.Run)
local walk = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.Parent.Animations.Walk)
local pos
for i,v in pairs(game.Players:GetPlayers()) do
		if v and v.Character then
			spawn(function()
				while true do
					wait()
					pos = v.Character.Torso.Position
				end
			end)
			while true do
				wait()		
				if (v.Character.Torso.Position - script.Parent.Parent.Torso.Position).Magnitude <= 60 then	
					run:Play()
					walk:Stop()
					script.Parent.Parent.Humanoid.WalkSpeed = 30
					path:ComputeAsync(script.Parent.Parent.Torso.Position,v.Character.Torso.Position)
					if path.Status == Enum.PathStatus.Success then
						local points = path:GetWaypoints()
						for i,point in pairs(points) do
							if point.Action == Enum.PathWaypointAction.Jump then
								script.Parent.Parent.Humanoid.Jump = true
							end
						script.Parent.Parent.Humanoid:MoveTo(point.Position)
						if point.Position ~= pos then 
							script.Parent.Parent.Humanoid:MoveTo(pos)
							script.Parent.Parent.Humanoid.MoveToFinished:Wait() 
							if (v.Character.Torso.Position - script.Parent.Parent.Torso.Position).Magnitude > 60 then break end
						end
					end
				end
			end	
		end
	end
end

2 comments

0

It will always attack the first player in the list because it looks at the first player in the list and then gets locked in an infinite loop.

0

How would i fix that?