Check to see if you hit an NPC?

So I have a question here. And how to make a check on whether you hit the NPS or not? For example, I need to do so you hit the NPC, then he starts chasing you and after 20 seconds if the distance is long and it was 20 seconds, he spawns at its starting position, then if you hit him again, he will start chasing you again. How do I do this?

Can you please clarify what do you mean by β€œhit”? Like, touch the npc, attack the npc, or what?

2 Likes

like attack, something like you make the damage

  1. So, first you need to damage the npc and get who attacked:
    for this, I would recommend you to make a function that damages the npc and get the player with as a param of the function. Example:
local function DamageNPC(player, npcHumanoid, damage)
    --the player param you will use to make the other stuff
    return npcHumanoid:TakeDamage(damage)
end)
  1. Now you should make the NPC moves to the player, you can use humanoid:MoveTo() for example.
local function DamageNPC(player, npcHumanoid, damage)
    local character = player.Character or player.CharacterAdded:Wait()
    local HRP = character:WaitForChild("HumanoidRootPart")

    npcHumanoid:MoveTo(HRP.Position)

    return npcHumanoid:TakeDamage(damage)
end)
  1. And now you will use tick() to detect if 20 seconds passed, and move the npc to the start position:
local lastTime = 0
local function DamageNPC(player, npcHumanoid, damage)
    local character = player.Character or player.CharacterAdded:Wait()
    local HRP = character:WaitForChild("HumanoidRootPart")

 coroutine.wrap(function()
   while wait(0.1) do
    if tick() - lastTime > 20 then
         npcHumanoid.Parent:WaitForChild("HumanoidRootPart").CFrame = 
         Vector3.new(0,0,0) --Change this to the start position
        else
         lastTime = tick()
         npcHumanoid:MoveTo(HRP.Position) 
      end 
   end
end)()

    return npcHumanoid:TakeDamage(damage)
end)

Note - Im not that good with tick() so Im not sure if it will works properly

1 Like