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()
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
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”).
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
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.