Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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
  1. Start by inserting a Script into ServerScriptService
  1. 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
  1. Instead of having so many scripts you only need 1
  1. This will work for respawns as well because of ChildAdded
  1. This is more efficient

So hopefully this helped you.

Best of luck developer!

6 comments

0

line 8 you misspelt destroy

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.

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.

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.

0

Thank you already solve it, thank you very much.

0

np

Putting this in the for.

game.Workspace.Enemy:GetChildren()

It has destroyed me 1 but the others have not

This discussion is preserved as it was. New answers, comments, and votes are closed.