Script isn't detecting when humanoid dies

local function addFighter(f)
	table.insert(fighters, f)
	table.insert(active, f)
	fighter = f
	local humanoid = fighter:FindFirstChild('Humanoid')
	update()

	humanoid.Died:Connected(function()
	 table.remove(active, table.find(active, f))
	  print('someone died in team '..i..',',#active,'left')
	   if #active < 1 then
	       gui.MatchLost:Fire(frame.Name)
	     end
	  update()
   end
end)


Not sure if this will work but you can try it I guess.

Unfortunately won’t work, I already mentioned that I’ve tried using Humanoid.Died.

I also added local humanoid = fighter:FindFirstChild(‘Humanoid’), which you could try.

This has worked for me in the past anyway.

From what i have understood so far, you are trying to change the health of the Humanoid from a Client sided way, which is not possible since it won’t actually change the Server Sided(whatever is in Workspace) value of the humanoid.Health,

I did put a block inside the workspace, with a Humanoid element inside of it, then i did create a script in the workspace and wrote this code inside,

local humanoid = game.Workspace:WaitForChild("HardNinjaGreatorDev2"):FindFirstChild("Humanoid")
local min_health = 0
local died = false

while died == false do 
	if humanoid.Health <= min_health then 
		print("i died")
		died = true
	end
	wait(1)
end

then i did create a script inside workspace to change the humanoid health into 0 after 5 seconds

wait(5)
game.Workspace.HardNinjaGreatorDev2.Humanoid.Health = 0 

After 5 seconds into the game, it printed i died and then it stopped, which means it went through.

If you want to handle things from client side to server side you will need to use Remote Events to handle the changes.

2 Likes

The problem you are facing is because you are changing the NPC’s health from a Client Sided way (ex. Local Script, a script inside ScreenGui or player related instead).

I would recommend you to take a look on Remote Events and try to get a better understanding between Server Side and Client Side before you continue, since it will help you a lot with the kind of game you are making

Actually I’m not trying to change the health, I am just trying to detect what the health is and when the humanoid dies from the client. I have tried making a remote event which also does not work. I made a new topic regarding that here.

its because the code that sets the humanoid health to 0 is made from client side and not server side

can u add me to a team create and show me what you made? I need to get a better understanding because i am sure the problem starts from somewhere else

1 Like

also because they didnt put the remote event in replicated storage

1 Like

The remote event can be anywhere server sided tbh, as long as both client side and server side can see it

1 Like

There is a function for when the humanoid dies that might work better then detecting its health

You could try using Humanoid:GetPropertyChangedSignal("Health"):Connect()

The problem on the code is not that it doesn’t find if the humanoid has died or not, the problem is that he is trying to change the health value of the fighter(npc) through a local script on the server side, which is not possible to do

He is not changing the health, though; he is checking for a health change, and then once it is lower or equal to zero,

It will run the rest of the code.

Yes, and i am saying the problem is from where he’s changing the health from, if the script is a local script and the actual NPC character is inside workspace, the actual humanoid health won’t change on the NPC, it will only change locally on the player, meaning it will never run the check since the actual health of the NPC won’t change to nil

Hm, the only way he’d be able to do this then is to make a server-sided script.

and once a NPC dies, it will fire a client event.

Humanoid.Died is there to save you

He has already tried it. It doesn’t work.

Like HardNinjaGreatorDev said, it’s a local script, which is probably what is causing the issue.

it being a localscript isn’t the issue, I just tried it, and the Died event works flawlessly