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
end
end
return target
end
while true do
wait(0.5)
local torso = findPlayer()
if torso then
print("Going to the player!")
enemyHumanoid:MoveTo(torso.Position, torso)
end
end