Humanoid.HealthChanged doesn't work?

im using a humanoid.healthchanged on an object which i gave a humanoid to it and it doesnt work

image

What have you done to debug this problem?

tried to print the health but it doesnt print the health whatsoever even when it decreases through server input

Try warning the health.Value instead

Have you done anything to verify the connection of your event listener to Humanoid.HealthChanged?

how do i do thatㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ

There is no reason why RBXScriptSignal:Connect would fail, so the best way to ensure it’s being reached is to place a print call prior to your connection of the event listener. You could also set a breakpoint and see if it’s triggered

i think the humanoid fails to reach the condition of humanoid.HealthChanged being triggered, and with breakpoints, doesnt run

This is a sign that your code isn’t being executed. You’ll need to show us more


ive used the function for this sentry, which has a humanoid and a humanoidrootpart as its primary part

can you show us the full script?

AttackTesting script:

local Players = game:GetService("Players")

local sentry = script.Parent

local config = script.Parent:WaitForChild("Config")
local cooldown = config:GetAttribute("SentryFirerate")
local damage = config:GetAttribute("SentryDamage")
warn(cooldown, damage)

local function FindNearestTarget()
	local maxDistance = config:GetAttribute("SentryRange")
	local nearestTarget = nil
	
	local targetList = {}
	for i, model in ipairs(workspace:GetDescendants()) do
		-- Checking to see if the target is fitting to be attacked by the sentry
		if model:FindFirstChild("Humanoid") and model:FindFirstChild("HumanoidRootPart") and model.Parent ~= workspace.Sentries and config:GetAttribute("Owner") ~= model.Name then		
			table.insert(targetList, model)
		end
	end
	
	for i, target in ipairs(targetList) do
--		local target = player.Character
		local targetDistance = (sentry.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
		if targetDistance < maxDistance  then
			nearestTarget = target
			maxDistance = targetDistance
			print("Target is " .. maxDistance .. " studs away from the sentry.")
		end
	end
	
	return nearestTarget
end


while true do
	local target = FindNearestTarget()

	if target then
		target.Humanoid:TakeDamage(damage)
		
	end
	task.wait(cooldown)
end

sentry.Humanoid.HealthChanged:Connect(function(health)
	warn(health)
	if health <= 0 then
		sentry:Destroy()
	end
end)

sentry.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	warn("among us")
end)

bottom

Ah! I see! Lines of code won’t run if while loop runs. You have to put the while loop into task.spawn!

task.spawn(function()
while true do
end
end)

1 Like

will coroutine.wrap work or is there a difference

You need to clarify that the humanoid hitbox is within the model’s boundaries. And you need to make sure that the sentry is a humanoid object through the script.

It works too! You can choose between coroutines and task library.

1 Like

Coroutine just makes your script look cooler. While true would work fine.

@Fan_Len4ika1Tvink123 has said that a coroutine or a task.spawn is required if we are to get another function to run outside the main loop

Yes i saw it already, no need for clarification :+1:t6:

Update: it won’t work with coroutines so i just used task.spawn()