My function is warning me?

Hello everyone! I have a function that sets a player’s health to a specific number that doesn’t seem to be working.

Here is my code:

local function SetHealth(NumVal)
	Health.Value = NumVal
end

And a screenshot of the error:
healthvalue

All of the Health variables are warning me. Any idea why, DevForum community?

2 Likes

Do you have this variable declared anywhere?
Are you sending this variable as a parameter?

Here is where I declare the variable:
pulseval
Not sure what you mean with the rest. I am a beginner scripter.

Health would need to be outside of the PlayerAdded where the function is for it to be a variable that the function can use. Also, you need to put local before health on the Health = PulseValue:Clone()

Yeah, you should declare it outside the variables. Where have you declared pulsevalue?

game.Players.PlayerAddes:Connect(function(PlayerAdded)
    Health = PulseValue:Clone()    -- Probably Should be local
    Health.Parent = PlayerAdded
end)

local function SetHealth(NumVal)
    Health.Value = NumVal    -- You no longer have access to Health here 
end

You should get the Health Object from the Player by passing a plr into the SetHealth Function since SetHealth does not know which plr it is targeting

local function SetHealth(NumVal, plr)
    local HealthValue = plr:FindFirstChild("Health")

    if HealthValue then
        HealthValue.Value = NumVal
    end
end

To find the Health Object it will need a name, this can be done in the PlayerAdded after you clone the object by using Health.Name = “Health”

local Health

game.Players.PlayerAdded:Connect(function(PlayerAdded)
    Health = PulseValue:Clone()
    Health.Parent = PlayerAdded
end)

local function SetHealth(NumVal)
    Health.Value = NumVal
end
1 Like

That’s because you don’t have a variable for the health.
Try to use this:

local Health = -- The Health's Value location - Detect the health value here

local function SetHealth(NumVal)
	Health.Value = NumVal
end
local Health

game.Players.PlayerAdded:Connect(function(PlayerAdded)
    Health = PulseValue:Clone()
    Health.Parent = PlayerAdded
end)

local function SetHealth(NumVal)
    Health.Value = NumVal
end

Not sure if this would work since the ‘local Health’ variable will be getting overridden each time a new player joins. This means the SetHealth function only has access to the latest player.

I think the Health value needs to be found from the player object of which you are trying to set the health for. This can be done using player:FindFirstChild(“ObjectName”).

Oh yeah, that would be an issue. Though I don’t see what the SetHealth function would do anyways. It’s supposed to set the health of someone?

local function SetHealth(plyr, NumVal)
    Player.Health.Value = NumVal
end

The function should probably look like this to make sure to check for errors when getting the HealthValue Object. Always good to check that an object exists when finding them before trying to modify their values.

Also remember that when finding an object it will need to be named. In the PlayerAdded you can do Health.Name = “Health”

local function SetHealth(NumVal, plr)
    local HealthValue = plr:FindFirstChild("Health")

    if HealthValue then
        HealthValue.Value = NumVal
    end
end

It should error otherwise. Not handling missing instances as errors can lead to further confusing and hard to fix bugs in your code.

Yeah. In the example, I am using this for an alternative to the default Roblox health system, so I have a few functions and health is one of them. I plan to open-source it.

Here is a link to another topic I have on this: