Function getting the wrong Argument

  1. What do you want to achieve? Change a NumberValue’s Value everytime a Character’s Humanoid’s health is changed

  2. What is the issue? Whenever i give the function the Humanoid Instance, it goes through as the Humanoids Health

  3. What solutions have you tried so far? I can’t make sense out of what is going wrong lol

Code:

local ChangeMulti = function(Humanoid,Value)
	print(Humanoid)
	local Health = Humanoid.Health
	local MaxHealth = Humanoid.MaxHealth
	Value.Value = Health/MaxHealth
end
-- I know this is a unreliable way to get the Character, but i made it simple to focus on the part of the code that isn't working
local Character = game.Workspace:WaitForChild("Tiagogibbon2")
local Humanoid = Character:WaitForChild("Humanoid")

Multiplier = Instance.new("NumberValue")
ChangeMulti(Humanoid,Multiplier)
Multiplier.Parent = game.Workspace
Humanoid.HealthChanged:Connect(ChangeMulti,Humanoid,Multiplier)

wait(1)

Humanoid:TakeDamage(25)

Error message: 3: attempt to index number with ‘Health’
(I get it when the humanoid health changes)

Script is on ServerScriptService btw

Your problem is with how you’re using the Connect method on the HealthChanged event. The Connect method only takes a function (no arguments after it), so you would want a “wrapper” function that calls your function with the arguments you want.
Something like this:

Humanoid.HealthChanged:Connect(function()
    ChangeMulti(Humanoid, Multiplier)
end)
1 Like