Healthchanged not firing locally

(script is in a gui, trying to tween the gui to the boss’s health when it gets damaged)

CODE:

if game.ReplicatedStorage.Bosses:FindFirstChild("Brute") then
	Hum = game.ReplicatedStorage.Bosses.Brute.Humanoid
elseif game.Workspace.BOSSES.Brute.Boss:FindFirstChild("Brute") then
	Hum = game.Workspace.BOSSES.Brute.Boss.Brute.Humanoid
end

local BossFolderBRUTE = game.Workspace["The Game"].BOSSES.Brute


local Gui = script.Parent.Under
local bar = Gui.Top

local function ChangeHP()
	
	print("HP CHanged.")
	script.Parent.Under.PI.Text = math.floor(Hum.Health)
	local Change = (Hum.Health / Hum.MaxHealth) - .029
	
	bar:TweenSize(
		UDim2.new(Change, 0, .763,0),
		Enum.EasingDirection.Out, 
		Enum.EasingStyle.Linear,
		0.25, 
		false
	)
end

spawn(function()
	print("Here")
	Hum.HealthChanged:Connect(function()
		print("Health changed lol")
		ChangeHP()
	end)
end)


BossFolderBRUTE.ChildAdded:Connect(function(Child)
	if BossFolderBRUTE:FindFirstChild("Brute") and Child.Name == "Brute" then
		print("LOL OK")
		Hum = BossFolderBRUTE.Brute:WaitForChild("Humanoid")
		ChangeHP()
	end
end)

Hum.Died:Connect(function()
	script.Parent.Enabled = false
end)

I have looked for solutions on the devforum and found none. Also, the initial ChangeHP() runs when the boss spawns. No errors, from the script.

If anyone knows why, please let me know.

I can’t seem to find any issues with the code looking at it, but I’ll try to do the best I can.
First, I need more info like:

  • Is the code just not firing?
    * Does it fire sometimes?
  • Does anything in the code work like it should?

You answered all of these, my bad.

The only thing I can think of is this section:

Provided the first if statement fires first, is the boss cloned to workspace, or is it parented to it? If it’s cloned, then you need to redirect the hum variable to the cloned one that you parented to workspace.

Everything works BUT the healthchanged.

Sounds good - can you answer to this section of the comment that I made?

Also, do you know which if statement is being fired? If you cloned the boss to workspace, than the first one will always fire, and you are referencing the wrong boss.

I believe that the boss is cloned.

I also DID redefine the humanoid variable.

Ok, after re-reading your code, I’m seeing some inconsistencies with the parenting scheme as these line:

and

which changes hum to:

All seem to be referencing different locations? Do you have multiple bosses in your game?

Yes, but only 1 is the brute, the other ones are folders.

And one I forgot to change, but it doesn’t really affect the code.

That’s actually the issue. Just tested it.

The issue could possibly be that your spawn function is happening BEFORE you change Hum to the correct variable (Since you don’t have a wait for the spawn before it fires). The spawn function is using the old variable that you had before changing it in the function after spawn is called. Try changing the Hum variable to the correct one before firing the ChangeHP function.

Honestly, it doesn’t even look like you need that spawn, and you can instead do this:

And you get the same effect without extra lines of code.

Why don’t you use :GetPropertyChangedSignal? Also, if you can’t use that, try passing the health arguments that you get in HealthChanged into the ChangeHp function.

Hum.HealthChanged:Connect(function(health-- Humanoid's health after being changed)
     -- Do some stuff
end)

Ok, so I put healthchanged inside of the childadded, and it seems to work.

1 Like