Hello, good afternoon, I want to eliminate the NPCs when they die, I have seen here a question that said how to do it I fixed the code, I read all about the RemoteEvent and I do not know what I'm doing wrong, I have the NPCs that are a model within that model I have 2 parts, which are the PathfindingService locations, this is the Npc model and denote a LocalScript with this.
local event = game.ReplicatedStorage.DiedEnemy event.OnClientEvent:Connect(function() print("Humaoid died!") end)
And in the Workspace I have a script with this.
wait(3) local enemy = game.Workspace.Enemy.Enemy local event = game.ReplicatedStorage.DiedEnemy function Dead() enemy.Parent:Destroy() event:FireAllClients() end enemy.Humanoid.Died:Connect(Dead)
Thanks and see if I solve it with your help.
Alright, I'm BlackOrange and I will be helping you today.
Your problem is, your checking if the humanoid has died on the Client
. Since your doing this chances are you are going to copy paste this local script into all the npc's which is bad as well.
So here is the solution:
Remove ALL local scripts that detect death
Start by inserting a
Script
intoServerScriptService
Start typing. I will guide you as I help.
local function CheckForDeath(Enemy) -- this function will check for death Enemy.Humanoid.Died:Connect(function() -- this will check if the humanoid dies Enemy:Destroy() end) Enemy.Humanoid.HealthChanged:Connect(function(Health) -- this is backup in case died doesn't work which i find buggy wait(1) -- this waits to see if the health regenerates if Health <= 0 then Enemy:Destroy() end end) end for _, enemy in pairs(game.Workspace:GetChildren()) do -- this line goes through all the children of workspace if enemy:IsA('Model') and enemy:FindFirstChild('Humanoid') and not game.Players:GetPlayerFromCharacter(enemy) then -- this line checks if the thing has a humanoid and if it's not a player CheckForDeath(enemy) end end workspace.ChildAdded:Connect(function(Enemy) -- if you ever respawn the enemy then this will check it again if enemy:IsA('Model') and enemy:FindFirstChild('Humanoid') and not game.Players:GetPlayerFromCharacter(enemy) then -- this line checks if the thing has a humanoid and if it's not a player CheckForDeath(Enemy) end end)
Now the good thing about this is:
Your doing this on the server so you don't need remote events
Instead of having so many scripts you only need 1
This will work for respawns as well because of ChildAdded
This is more efficient
So hopefully this helped you.
Best of luck developer!
Putting this in the for.
game.Workspace.Enemy:GetChildren()
It has destroyed me 1 but the others have not