Is there a way i can set the health of a player by just adding a value inside the character model?
Im trying to have a single script use a value to store the players health.
Humanoid.Health is just fine. I do not see why you wish to do this.
If this is not it can you explain it a little more? But if you just want to get the players health and change it you can do this:
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
if humanoid then
print(humanoid.Health) -- print the players health
humanoid.Health = 50 -- sets player health to a specific amount
humanoid:TakeDamage(20) -- makes player take 20 damage
-- your final health would be 30.
end
end)
end)
(btw I just used PlayerAdded and CharacterAdded to get their character just to demonstrate how you could find and change the players health, you don’t have to use this to get the player/character)
Oh I just reread the post, I don’t think there would be a reason to do this like @Ziffixture said as Humanoid.Health exists, but if you really need to, I can show you how.
I guess what im trying to do is use a IntValue to store the players health. If that makes sense.
Oh okay, this might work:
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:WaitForChild("Humanoid")
local healthValue = Instance.new("IntValue")
healthValue.Name = "HealthValue" -- whatever you want lol
healthValue.Value = humanoid.Health
healthValue.Parent = character -- put it wherever u want
healthValue:GetPropertyChangedSignal("Value"):Connect(function()
humanoid.Health = healthValue.Value
end)
-- just to test it
while task.wait(1) do
healthValue.Value -= 10
end
end)
end)
edit: oops sorry I forgot to parent it, but just put it or name it where ever or whatever you want.
Yeah, pretty much what @Ziffixture explained. Humanoid.Health
Why though? Is there a particular reason you wouldn’t be able to reference Humanoid.Health?
Also just to add on, you could also try using Humanoid.HealthChanged as well if you wanted to have it change regardless of if the value or the Humanoid.Health is changed. Although the solution above achieves what you’d like too.
Ohhhhh, now I get it. So, you just want to store it if I’m correct?
Well, here:
IntValue.Value = Humanoid.Health; -- Setting the value of the IntValue to the humanoid's health. It'll just stay stored and will not change.
Unless, if you want to change the value when the Health changes, you might want to use Humanoid.HealthChanged as @alphadoggy111 said.
Also if you want the IntValue to change when the health changes, you could do what @alphadoggy111 said and use Humanoid.HealthChanged. Sorry I had to go to bed so I can’t really write it right now but you would add something like this after the healthValue:GetPropertyChangedSignal function:
humanoid.HealthChanged:Connect(function(health)
healthValue.Value = health
end)