Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to increase health based off an stat value?

I want to make it so when a player gains strength their health also goes up. I'm a new scripter so I have no idea what I'm doing and I'm stuck.

This is what i tried so far but the health does no go up, nothing in output either.

local DefaultHealth = 100

game.Players.PlayerAdded:Connect(function(player)
	local Stats = player:WaitForChild("leaderstats")
	local Strength = Stats:FindFirstChild("strength")
	local char = player.Character or player.CharacterAdded:wait()
	if char then
		local hum = char:FindFirstChild("Humanoid")
		if hum then
			hum.MaxHealth = DefaultHealth * Strength.Value
		end
	end
end)

update: I had this script above in a local script under starter character so i moved it into a script under server script and when i tested, the players maxhealth went to 0.

2 comments

0

maybe the Strength.Value is 0?

0

the player has over 2000 strength tho so how can that be?

2 answers

If you have a script that doesn't increase the strength value, the value is being multiplied by zero. Add a script that increases the strength value, and you might be rid of the error.

2 comments

0

i have a button that gives strength just for testing and no matter how much strength i have the health doesn't go up

0

oh

Got it working, thanks for the help.

updated script:

local DefaultHealth = 100

game.Players.PlayerAdded:Connect(function(player)
	local Stats = player:WaitForChild("leaderstats")
	local Strength = Stats:FindFirstChild("strength")
	local char = player.Character or player.CharacterAdded:wait()
	if char then
		local hum = char:FindFirstChild("Humanoid")
		if hum then
			hum.MaxHealth = DefaultHealth * Strength.Value
		end
		
		Strength.Changed:connect(function(value)
			if player.Character and player.Character:FindFirstChild("Humanoid") then
				player.Character.Humanoid.MaxHealth = DefaultHealth + Strength.Value * 1.25
			end
			
			player.CharacterAdded:connect(function(character)
				character:WaitForChild("Humanoid").MaxHealth = 100 + Strength.Value * 1.25
			end)
		end)
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.