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
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)
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.
What is the issue? Include screenshots / videos if possible!
Also, that is the youtube tutorial that i looked to make the script:
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.
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
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)```
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)
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
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)
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