Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

(Solved)How can I destroy the NPC with a RemoteEvent in the working space?

Asked by
brok4d 77
5 years ago
Edited 5 years ago

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.

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Alright, I'm BlackOrange and I will be helping you today.

Problem:

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:

Solution:

  1. Remove ALL local scripts that detect death

  2. Start by inserting a Script into ServerScriptService

  3. 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:

  1. Your doing this on the server so you don't need remote events

  2. Instead of having so many scripts you only need 1

  3. This will work for respawns as well because of ChildAdded

  4. This is more efficient

So hopefully this helped you.

Best of luck developer!

0
line 8 you misspelt destroy ScrubSadmir 200 — 5y
0
Thank you very much for your work and your time but it does not work for me, but thank you very much, I think it will be because the model of the npc is inside the model that contains the 2 parts of the routes that it follows. brok4d 77 — 5y
0
Nor is that because I have removed the npc from the main model in the workspace and it does not work either, it does not destroy it, many thanks anyway and I am sure it will be for something and I will discover it. I already have a great guide of where things are going. brok4d 77 — 5y
0
Brok3 just make sure your enemy is a model in workspace. The model has a Humanoid in it and tthat's all it takes. BlackOrange3343 2676 — 5y
View all comments (2 more)
0
Thank you already solve it, thank you very much. brok4d 77 — 5y
0
np BlackOrange3343 2676 — 5y
Ad
Log in to vote
0
Answered by
brok4d 77
5 years ago
Edited 5 years ago

Putting this in the for.

game.Workspace.Enemy:GetChildren()

It has destroyed me 1 but the others have not

Answer this question