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)
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")
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
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.
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