Enemy script is not working


Hi scripters,

i have a problem with my enemy script. It runs just once. Like it comes to the player and then it won’t move anymore.

Here is my code:

--Setting up variables
local enemyTorso = script.Parent.Torso
local enemyHumanoid = script.Parent.Humanoid
local minDistance = 40

--Finding players in range of 40 studs
local function findPlayer()
	local target
	for i,v in ipairs(game.Players:GetPlayers()) do
		local character = v.Character or v.CharacterAdded:Wait()
		local humanoidRoot = character:WaitForChild("HumanoidRootPart")
		if v:DistanceFromCharacter(enemyTorso.Position) < minDistance then
			minDistance = v:DistanceFromCharacter(enemyTorso.Position)
			target = humanoidRoot

	return target

while true do
	local torso = findPlayer()
	if torso then
		print("Going to the player!")
		enemyHumanoid:MoveTo(torso.Position, torso)

Here is a link that shows the bug: New VideoForm

you reduce the minDistance everytime it check player just delete that line and it should work fine

1 Like

Sorry for the late reply. But ill try it

Thx i was so stupid for not realising it :sweat_smile:

1 Like