:GetPropertyChangedSignal() passing nil to function

Alright, so I’m trying to make a more customizable health regeneration system from the default one roblox is using, but for some reason whenever any of the :GetPropertyChangedSignals() fire, they just pass nils to the healthRegen() function. Anybody know why this is happening, and so could you please kindly tell the solution to this problem? Thank you.

The error:

image

local regenRate = 1/100
local playersHealing = {}
local regenStepTime = 1
local runService = game:GetService("RunService")
local plrs = game.Players

function removeItemFromTable(t, i)
	for i,v in t do
		if v == i then
			table.remove(t, i)
			break
		end
	end
end

function healthRegen(hum : Humanoid)
	local char = hum.Parent
        if table.find(playersHealing, char.Name) ~= nil then return end
	if hum.MaxHealth > hum.Health then
		table.insert(playersHealing, char.Name)
		repeat
			if 0.01 > hum.Health then removeItemFromTable(playersHealing, char.Name) return end
			if not hum or char.canHealthRegen.Value == false then removeItemFromTable(playersHealing, char.Name) return end
			task.wait(regenStepTime)
			hum.Health = math.min(hum.Health + regenStepTime*regenRate*hum.MaxHealth, hum.MaxHealth)
		until hum.Health >= hum.MaxHealth
		removeItemFromTable(playersHealing, char.Name)
	end
end

plrs.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local robloxHealthScript = char:WaitForChild("Health")
		if robloxHealthScript then
			robloxHealthScript:Destroy()
		end
		local canHealthRegen = Instance.new("BoolValue", char)
		canHealthRegen.Name = "canHealthRegen"
		canHealthRegen.Value = true
		local hum = char:FindFirstChildOfClass("Humanoid")
		hum:GetPropertyChangedSignal("Health"):Connect(healthRegen)
		hum:GetPropertyChangedSignal("MaxHealth"):Connect(healthRegen)
		canHealthRegen:GetPropertyChangedSignal("Value"):Connect(healthRegen)
    end)
end)
1 Like

That’s just the way :GetPropertyChangedSignal() is!
You’ll have to reaccess the value within the handling function.

1 Like

What exactly do you mean by that?

In my function it’s waiting for a value that represents a humanoid.

However, the value returns nil when the function runs.

Thanks.

The connections for GetPropertyChangedSignal() does not return anything. You would have to create another function to pass your hum variable.

local function changed(): ()
    healthRegen(hum)
end

hum:GetPropertyChangedSignal("Health"):Connect(changed)
hum:GetPropertyChangedSignal("MaxHealth"):Connect(changed)
canHealthRegen:GetPropertyChangedSignal("Value"):Connect(healthRegen)

Ok thanks for letting me know about this.

Albeit it might be a little messier, I’ll just use a :Connect(function() instead now that I know that. Thank you

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.