How to make server wide death script

So i want to make it where if any humanoid in the entire workspace dies then it fires this script :

Enemy.Died:Connect(function()
local TweenService = game:GetService("TweenService")
for i, v in ipairs(Children) do
	if  v.ClassName == "Part" or v.ClassName == "MeshPart" then
		v.Anchored = true
		v.CollisionGroupId = 1
		v.Color = Color3.fromRGB(65, 62, 64)
		local tweeninfo = TweenInfo.new(2) -- how long you want them to take 
		local Goal = {}
		Goal.Transparency = 1
		local tween = TweenService:Create(v, tweeninfo, Goal)
		tween:Play()
		tween.Completed:Connect(function(playbackState)
    		v:Destroy()
		end)
	end
end
end)

currently i just have been puting them by hand in each enemy , resulting in 20+ of this same script . I was hoping someone knew how to use only a Single script to achive the same affect

There are a couple ways to achieve this. The easiest would likely be to run this code each time an enemy is spawned. If the enemies aren’t spawned by your code, you could use other options to run this code over all of the NPC’s such as running a for loop if they share a parent over the children of that parent, or using Collection Service.

I’m a bit confused on what your trying to achieve, if you could clarify, that would be nice.

Try using a table and putting all the enemies/players into that table, if they die, remove them from that table. Example:

local enemies = {}

for i = 1, 20 do -- Number of enemies to add (Ignore this if you already have an enemy adding system.)
	local enemy1 = game.ServerStorage.Enemy -- Enemy location.
	local enemy1Clone = enemy1:Clone()
	enemy1Clone.Parent = game.Workspace
	table.insert(enemies, #enemies, enemy1Clone) -- Add the enemy to the table.
end

for _, enemy in pairs(enemies) do
	enemy.Humanoid.Died:Connect(function() -- When the enemy dies do the following.
		
		table.remove(enemies, table.find(enemies, enemy)) -- Remove the enemy from the table.
		
		if enemies[1] == nil then
			-- Initiate what happens when 0 enemies.
		end
	end)
end

ok so basicly im trying to make it where the script that scans the whole workspace for any humanoids, so i dont have to put each one into a table . For example , if i throw a random humanoid into the workspace , the death effect will still play . no need for individual script .

Are the enemies spawned in by script, or there by default?

they are placed by hand / placed by default

If you want to check if any Humanoid in the Workspace dies, try using :IsA(“Humanoid”)

local FIRE_ONCE = false

for _, object in pairs(game.Workspace:GetDescendants()) do -- Searches through every child of the workspace.
	wait()
	if object:IsA("Humanoid") then
		object.Died:Connect(function()
			if FIRE_ONCE == true then return end -- Will disconnect the function.
			
			FIRE_ONCE = true -- So it will never fire again.
			
			-- What happens when the Humanoid dies
		end)
	end
	
end

Alright, so do they all share a common parent?

I advise against that. Great way to create a hang and/or unneeded lag.

does this work for all descendants? for example if the humanoid is hidden in a folder somewhere would it still work?

It searches through all descendants. I updated the code. It goes through EVERY single child of workspace

can u elaborate on that? like how that would cause unneeded lag

It’s not going to cause too much lag, but in terms of how many times you call the function it can get laggy.

Especially if you have a lot of parts in studio.

Well first off, your iterating through what could range from 1k to 50k parts. Lets assume 30k parts are in your workspace. Add some decals and textures and misc stuff, we will say 40k. So your going to iterate over 40 thousand instances without any form of wait(). This will almost surely cause a hang.

Ah yes i forgot to put a wait, give me a second…

The better way to do it, is to put a script under each humanoid, tag it in collection service or something, then get all of the tagged humanoids. Then there is no need to check 49,950 other irrelevant instances.

This is what he was trying to avoid. Also I’m doing a simple check not functions on each part. This causes little to no lag if called once.

so each model has about 15 parts (R15 Models) each cave will have less than 60 mobs so . that would only add up to 900 parts . would it still lag?

If you just want to search a specific folder put the folder location in replacement of game.Workspace

I think he was trying to avoid violating the D-R-Y principle, and repeating his code when not necessary. Another option is to have a module with that function, and call it in each script which can be manually placed under the humanoid. I’m telling you iterating over all of workspace’s descendants is a pretty bad idea, but do as you wish.

1 Like