I’m trying to make an enemy for my game. It’s working well, but after the enemy hurts the player once, it stops following.
The script for following the player:
local entity = script.Parent
local entityHuman = entity:WaitForChild("Humanoid")
local entityHRP = entity:WaitForChild("HumanoidRootPart")
local players = game:GetService("Players")
local runService = game:GetService("RunService")
local function move()
local target = nil
local playerInGame = players:GetChildren()
for i, plr in pairs(players:GetChildren()) do
local char = plr.Character
if char then
local plrHRP = char:FindFirstChild("HumanoidRootPart")
if plrHRP and (entityHRP.Position - plrHRP.Position).Magnitude < 50 then
if target then
if (entityHRP.Position - target.Position).Magnitude > (entityHRP.Position - plrHRP.Position).Magnitude then
target = plrHRP
end
else
target = plrHRP
end
end
end
end
if target then
entityHRP:SetNetworkOwner(nil)
entityHuman:MoveTo(target.Position)
end
end
runService.Stepped:Connect(move)
The script for hurting the player:
local myRhand = script.Parent["Right Lower Arm"]
local damage = true
local Nil
myRhand.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if damage == true then
damage = false
hit.Parent.Humanoid:TakeDamage(40)
wait(5)
damage = true
elseif damage == false then
Nil = "ok"
end
end
end)