local npc = script.Parent
local debounce = false
local wasAttacked = false
local wasDistance = false
npc.Humanoid.Touched:Connect(function(hit)
if hit and game.Players:GetPlayerFromCharacter(hit.Parent) then
if debounce == false then
debounce = true
hit.Parent.Humanoid:TakeDamage(10) -- U SHOULD TO CHANGE THIS!!!
wait(.25) -- AND THIS!!!
debounce = false
end
end
end)
function FindPlayer(Position)
local list = game.Workspace:GetChildren()
local torso = nil
local distance = 50 -- CHANGE THIS TOO!!!
local HRP = nil
local humanoid = nil
local player = nil
for i = 1, #list do
player = list[i]
if (player.ClassName == "Model") and (player ~= script.Parent) then
HRP = player:FindFirstChild("HumanoidRootPart")
humanoid = player:FindFirstChild("Humanoid")
if (HRP ~= nil) and (humanoid ~= nil) and (humanoid.Health > 0) then
if (HRP.Position - Position).Magnitude < distance then
torso = HRP
distance = (HRP.Position - Position).Magnitude
if distance <= 40 then
wasDistance = true
else
wasDistance = false
end
end
end
end
end
return torso
end
npc.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
wasAttacked = true
while wasAttacked == true or wasDistance == true do
wait(1)
local target = FindPlayer(script.Parent.HumanoidRootPart.Position)
if target ~= nil then
script.Parent.Humanoid:MoveTo(target.Position, target)
end
end
end)
--while true do
-- wait(1)
-- local target = FindPlayer(script.Parent.HumanoidRootPart.Position)
-- if target ~= nil then
-- script.Parent.Humanoid:MoveTo(target.Position, target)
-- end
--end
So this is my script at the enemy. at the moment when I hit him he follows me, and when the distance is large does not stop going, but the problem is that I do not know how to do the following:
- If I do not hit him for more than 10 seconds, for example, he returns to his spawnpoint
- If I am away from him a lot, he also returns to his spawnpoint
- If I hit him, he starts to follow me.
Can you tell me how it can be done?