HumanoidRootPart still follows the Player after death

I made this script by using a tutorial on youtube, the script actuallly works very well, but there is a problem with it, even after the humanoid dies, the HumanoidRootPart still follows the Player, this is pretty annoying the player actually can collide with the HumanoidRoot Part. If someone tries to solve it or give me a hint on where i can solve the problem, i will be Grateful. Thanks :heart:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local EnemyCharacter = script.Parent
local EnemeyHumanoid = EnemyCharacter.Enemy
local Deboucne = false
local AttackDistance = nil

EnemeyHumanoid.Touched:Connect(function(hit)
	local Character = hit.Parent
	local Player = Players:GetPlayerFromCharacter(Character)
	
	if Player and Deboucne == false then
		Deboucne = true
		Character.Humanoid:TakeDamage(0)
		task.wait(0.25)
		Deboucne = false
	end
end)


local function FindClosestPlayer(Position)
	local ClosestPlayer = nil
	local ClosestDistance = math.huge
	
	for i, Player in pairs(Players:GetPlayers()) do
		local Character = Player.Character
		if Character then
			local HumanoidrootPart = Character.HumanoidRootPart
			if HumanoidrootPart then
				local Distance = (HumanoidrootPart.Position - Position).Magnitude
				
				if AttackDistance == nil or Distance <= AttackDistance then
					if Distance < ClosestDistance then
						ClosestPlayer = Player
						ClosestDistance = Distance
					end
				end
			end
		end
	end
	return ClosestPlayer
end

local function Attack(Player)
	local Character = Player.Character
	if Character then
		local HumanoidRootPart = Character.HumanoidRootPart
		
		if HumanoidRootPart then
			EnemeyHumanoid:MoveTo(HumanoidRootPart.Position, HumanoidRootPart)
		end
	end
end

RunService.Heartbeat:Connect(function()
	local ClosestPlayer = FindClosestPlayer(EnemyCharacter.HumanoidRootPart.Position)
	
	if ClosestPlayer then
		Attack(ClosestPlayer)
	end
end)
  1. What do you want to achieve? Keep it simple and clear!
  • I want in the exactly moment that the enemy dies, the HumanoidRootPart stops chasing the player.
  1. What is the issue? Include screenshots / videos if possible!

robloxapp-20240406-1042404.wmv (4.4 MB)

Sorry for the low quality video

Also, that is the youtube tutorial that i looked to make the script:

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
  • A bool value that if true, the humanoid walks into the player (Didn’t worked), An if statment that if humanoid died, the HumanoidRootPart position will be: Vector3.new() (Also didn’t worked), A if statment that if the Humanoid dies, the Humanoid Anchors and Un-Anchors itself (Didn’t work either).

Yes, i looked in topics on the devforum and in Developer Hub for someone with a similar or with the same problem, but all i found, was people with a random enemy problem.

2 Likes

You can just destroy the enemy when it dies:

local runService = game:GetService("RunService")

runService.RenderStepped:Connect(function()
    if EnemeyHumanoid.Health <= 0 then
        EnemyCharacter:Destroy()
    end
end)

Edit: just put

if EnemeyHumanoid.Health <= 0 then
        EnemyCharacter:Destroy()
    end

in your heartbeat area

It also don’t works either.

robloxapp-20240406-1212140.wmv (2.1 MB)

Updated Script:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local EnemyCharacter = script.Parent
local EnemeyHumanoid = EnemyCharacter.Enemy
local Deboucne = false
local AttackDistance = nil

EnemeyHumanoid.Touched:Connect(function(hit)
	local Character = hit.Parent
	local Player = Players:GetPlayerFromCharacter(Character)
	
	if Player and Deboucne == false then
		Deboucne = true
		Character.Humanoid:TakeDamage(0)
		task.wait(0.25)
		Deboucne = false
	end
end)


local function FindClosestPlayer(Position)
	local ClosestPlayer = nil
	local ClosestDistance = math.huge
	
	for i, Player in pairs(Players:GetPlayers()) do
		local Character = Player.Character
		if Character then
			local HumanoidrootPart = Character.HumanoidRootPart
			if HumanoidrootPart then
				local Distance = (HumanoidrootPart.Position - Position).Magnitude
				
				if AttackDistance == nil or Distance <= AttackDistance then
					if Distance < ClosestDistance then
						ClosestPlayer = Player
						ClosestDistance = Distance
					end
				end
			end
		end
	end
	return ClosestPlayer
end

local function Attack(Player)
	local Character = Player.Character
	if Character then
		local HumanoidRootPart = Character.HumanoidRootPart
		
		if HumanoidRootPart then
			EnemeyHumanoid:MoveTo(HumanoidRootPart.Position, HumanoidRootPart)
		end
	end
end

RunService.Heartbeat:Connect(function()
	local ClosestPlayer = FindClosestPlayer(EnemyCharacter.HumanoidRootPart.Position)
	
	if ClosestPlayer then
		Attack(ClosestPlayer)
	end
	
	if EnemeyHumanoid.Health <= 0 then
		EnemyCharacter:Destroy()
	end
end)```

I can’t also use RenderStepped Event cause it can only be used in a Local script.

It’s debounce. Also, the script that you used gave me nostalgia, because this script was used 3 years ago or more

Try adding a print statement inside of it

 EnemeyHumanoid.Health <= 0 then
	EnemyCharacter:Destroy()
    print("Enemy has been destroyed!")
end

lol sorry i really type bad, sorry :sweat_smile:

It still don’t work, i will try changing EnemyCharacter:Destroy to EnemyCharacter.HumanoidRootPart:Destroy()

The script is old. The main issue is RenderStepped didn’t stopping which can cause memory leak. So that’s new code.

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local EnemyCharacter = script.Parent
local EnemyHumanoid= EnemyCharacter.Enemy
local Debounce = false
local AttackDistance = nil

EnemyHumanoid.Touched:Connect(function(hit)
	local Character = hit.Parent
	local Player = Players:GetPlayerFromCharacter(Character)
	
	if Player and Debounce == false then
		Debounce = true
		Character.Humanoid:TakeDamage(0)
		task.wait(0.25)
		Debounce = false
	end
end)


local function FindClosestPlayer(Position)
	local ClosestPlayer = nil
	local ClosestDistance = math.huge
	
	for i, Player in pairs(Players:GetPlayers()) do
		local Character = Player.Character
		if Character then
			local HumanoidrootPart = Character.HumanoidRootPart
			if HumanoidrootPart then
				local Distance = (HumanoidrootPart.Position - Position).Magnitude
				
				if AttackDistance == nil or Distance <= AttackDistance then
					if Distance < ClosestDistance then
						ClosestPlayer = Player
						ClosestDistance = Distance
					end
				end
			end
		end
	end
	return ClosestPlayer
end

local function Attack(Player)
	local Character = Player.Character
	if Character then
		local HumanoidRootPart = Character.HumanoidRootPart
		
		if HumanoidRootPart then
			EnemeyHumanoid:MoveTo(HumanoidRootPart.Position, HumanoidRootPart)
		end
	end
end

local connection = RunService.Heartbeat:Connect(function()
	local ClosestPlayer = FindClosestPlayer(EnemyCharacter.HumanoidRootPart.Position)
	
	if ClosestPlayer then
		Attack(ClosestPlayer)
	end
end)
EnemyHumanoid.Died:Once(function()
     connection:Disconnect()
end)

Also changed EnemeyHumanoid to EnemyHumanoid

It Didn’t work. Can the game being on the R6 model interfere with anything?

No, HumanoidRootPart exist for both R6 and R15. I have question. Do you have another scripts in enemydummy. This can be the problem. Just wait a minute, I broke my roblox studio, after I repair it I will test the script

Try this

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local EnemyCharacter = script.Parent
local EnemeyHumanoid = EnemyCharacter.Enemy
local Deboucne = false
local AttackDistance = nil

EnemeyHumanoid.Touched:Connect(function(hit)
	local Character = hit.Parent
	local Player = Players:GetPlayerFromCharacter(Character)

	if Player and Deboucne == false then
		Deboucne = true
		Character.Humanoid:TakeDamage(0)
		task.wait(0.25)
		Deboucne = false
	end
end)


local function FindClosestPlayer(Position)
	local ClosestPlayer = nil
	local ClosestDistance = math.huge

	for i, Player in pairs(Players:GetPlayers()) do
		local Character = Player.Character
		if Character then
			local HumanoidrootPart = Character.HumanoidRootPart
			if HumanoidrootPart then
				local Distance = (HumanoidrootPart.Position - Position).Magnitude

				if AttackDistance == nil or Distance <= AttackDistance then
					if Distance < ClosestDistance then
						ClosestPlayer = Player
						ClosestDistance = Distance
					end
				end
			end
		end
	end
	return ClosestPlayer
end

local function Attack(Player)
	local Character = Player.Character
	if Character then
		local HumanoidRootPart = Character.HumanoidRootPart

		if HumanoidRootPart then
			if EnemeyHumanoid.Health == 0 then
				AAA:Disconnect()
			end
			EnemeyHumanoid:MoveTo(HumanoidRootPart.Position, HumanoidRootPart)
		end
	end
end

AAA = RunService.Heartbeat:Connect(function()
	local ClosestPlayer = FindClosestPlayer(EnemyCharacter.HumanoidRootPart.Position)
	if EnemeyHumanoid.Health <= 0 then
		AAA:Disconnect()
	end

	if ClosestPlayer then
		Attack(ClosestPlayer)
	end
end)

Yes there is a Animate one, a respawn and the Chase script (Script)
image

Show what is inside of respawn script

local Copy = script.Parent:Clone()
local Enemy = script.Parent
local Humanoid

local EnemyStatus = script.Parent:FindFirstChild("EnemyStatus")
local StatusModule = require(EnemyStatus)

for i,v in pairs(Enemy:GetChildren()) do 
	if v:IsA('Humanoid') then 
		Humanoid = v 
	end 
end

if Humanoid then
	Humanoid.Died:Connect(function()
		task.wait(StatusModule.Cooldown)
		Copy.Parent = Enemy.Parent
		Copy:MakeJoints()
		script.Parent:Destroy()
	end)
else
	warn("Humanoid wans't found!")
end

Oh, i just found out that is the problem with the tool im using. Thanks for everyone who helped me :).

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.