NPC doesn't know when Player Respawns

So I’ve made and NPC that tries to kill the player which works fine but when the player respawns it keeps attacking where the player just died.

I’ve been looking around and solutions from other posts dont work for me and I can never really make much sense of how they word things on creator hub

I’ll attach a video of the problem

And the script

-- local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")

local Bob = game.Workspace.Bob
local BobHum = Bob:FindFirstChildOfClass("Humanoid")
local BobHumRoot = Bob.HumanoidRootPart
local BobHead = Bob["Head Top"]

local Players = game:GetService("Players")
local Plr = game.Players.PlayerAdded:Wait()
local Char  = Plr.Character
if not Char or not Char.Parent then
	Char = Plr.CharacterAdded:Wait()
end
local PlrHum = Char:FindFirstChildOfClass("Humanoid")
local PlrHumRoot = Char:WaitForChild("HumanoidRootPart")
local Hand = game.StarterPlayer.StarterCharacter.RightHand
local plrAnimator = PlrHum:FindFirstChildOfClass("Animator")
local punchAnim = game.StarterPlayer.StarterCharacter["YaYa Animations"]["YaYa PUNCH"]


local ATK = Bob.Animations.BobATK
local IDLE = Bob.Animations["Bob Idle"]
local animator = BobHum.Animator
local ATKTrack = animator:LoadAnimation(ATK)
local IDLETrack = animator:LoadAnimation(IDLE)

local direction = (BobHumRoot.Position - PlrHumRoot.Position).Unit
local distance = (BobHumRoot.Position - PlrHumRoot.Position).Magnitude == 8

debounce = false

function onPlayerAdded(Plr)
	print(Plr.Name)
end

for _, 	Plr in Players:GetPlayers() do
	onPlayerAdded(Plr)
end


function lookAtPlr()
	local debounce = false
	
	if (BobHumRoot.Position - PlrHumRoot.Position).Magnitude < 20 then
		BobHumRoot.CFrame = CFrame.new(BobHumRoot.Position, PlrHumRoot.Position + direction)
		task.wait(0.5)
	end
end


function idle()
	if debounce then
		return
	end
	
	BobHumRoot.Anchored = true
	
	if (BobHumRoot.Position - PlrHumRoot.Position).Magnitude > 8 then
		debounce = true
		IDLETrack:Play()
		task.wait(1.5)
		debounce = false
	end
	if (BobHumRoot.Position - PlrHumRoot.Position).Magnitude <= 8 then
		IDLETrack:Stop()
		debounce = true
		pcall(ATK)
	end
end

function ATK()
	if (BobHumRoot.Position - PlrHumRoot.Position).Magnitude <= 8 then
		ATKTrack:Play()
		IDLETrack:Play()
		task.wait(2)
		debounce = false
		pcall(idle)
	end
end

function KillPlr()
	
	if ATKTrack.IsPlaying then
		if Plr:GetChildren() then
			PlrHum.Health = 0
		end
		
	end
end


Players.PlayerAdded:Connect(onPlayerAdded)
BobHead.Touched:Connect(KillPlr)
RunService.Heartbeat:Connect(idle)
RunService.Heartbeat:Connect(lookAtPlr)

robloxapp-20231206-1515133.wmv (1.7 MB)

1 Like

Where is this script being placed? are there any console errors?

1 Like

its in serverScriptService the only thing i get for an error is this
Screenshot 2023-12-06 164112

1 Like

instead of player added how about character added

1 Like

can you expand on that? because there is a character.added in there

1 Like

How about a time out on the waitforchild?
local PlrHumRoot = Char:WaitForChild(“HumanoidRootPart”,30)

if you want another way to get around this maybe the waitforchild error is there because the character is a nil

tell me your results

1 Like

no results there. the error is in the automated animate script for the player i dont know if that helps at all

1 Like

What type of Character is this? R6 or R15

1 Like

its a R6 character that i made. the infinite yield seems to start when the player starts moving

1 Like

The issue is that your character is R15, R15 characters don’t contain Torso, only UpperTorso and LowerTorso. You should probably do this, to make it so it is playable for R6 and R15. I am not sure where you get the Torso but this will work:

FindFirstChild:("UpperTorso") or FindFirstChild("Torso")
1 Like

no idea how that would be though since i made rigged it as R6 because its a custom character for the game or is scripting animations different between R6 and R15? the script is the automated one its not my script so i dont think i can edit that

1 Like
1 Like

im not doing a patrol system… the enemy stays in place. im sorry but did you view the video? i dont understand what you are trying to show me here

1 Like

this is a pathfinding script that follows the player and doesnt lose it after resetting

1 Like

im not pathfinding though. the NPC just looks at the player with cframe and then attacks when the player is close enough, it doesnt move so there is no pathfinding in the script.

1 Like

i noticed i had an extra part so i change to R15, and renamed the parts now theres no errors but the issue still remains

1 Like

ok so an update, i tested the game and printed the distance between the enemy and player after the player dies and the enemy still thinks the plr root part is in front of it. now i understand the problem but still not sure how to go about it

1 Like

i took out the debounce = false in the atk function now the attack doesnt repeat. i think it is stuck somewhere in the cframe loop

1 Like

its not detecting either the player or the player character.

1 Like

it didnt detect anyone so thats why i had to redo my get player function

1 Like