Hi,
How would I go about making a script that would: increase a player's health, as X leaderstat value increases?
If anyone could explain, I'd appreciate it as I'm still trying to learn.
Example: Player has 0 strength and 100 health. His strength leaderstat rises to 10, and along with it, his health also goes up to 150.
How would I go about this?
Thanks in advance!
local Player = game:GetService("Players").LocalPlayer local Strength = Player.leaderstats.Strength local Humanoid = Player.Character.Humanoid local function onStrengthIncrease() Humanoid.MaxHealth = Humanoid.MaxHealth + 10 Humanoid.Health = Humanoid.MaxHealth end Strength:GetPropertyChangedSignal("Value"):Connect(onStrengthIncrease)
The code above is simple. We have three variables, one for the player, one for the strength leaderstat, and one for the humanoid. We attach a function to an event that fires every time the strength leaderstat increases. If that's the case, we increase the maximum health in the humanoid by 10 and make the health of the humanoid equivalent to the maximum health. That's all!
just do this: add a remote event in replicated Storage then from a local script you should listen for changes in player's strength leaderstat, when there is a change in strength leaderstat then call the remote event so the server can update the player's max health; example:
-- in local script
local inc_Health = game.ReplicatedStorage.updateHealth local player = game.Players.LocalPlayer local strength = player.leaderstats.Strength strength.GetPropertyChangedSignal("Value"):Connect(function() inc_Health:FireServer(strength.Value) end)
--in server script the following code adds 5 health to the players max health every time their strength increase. to make it so the player only gets 50 health every ten times that the player's health increase then see the next code example
local inc_Health = game.ReplicatedStorage.updateHealth inc_Health .OnServerEvent:Connect(function(player,value) local char = player.Character local humanoid = char.Humanoid humanoid.MaxHealth = 100+(5*value) humanoid.Health = Humanoid.MaxHealth end)
to make it so the player only gets 50 health every ten times that the player's health increase
-- in server script
local inc_Health = game.ReplicatedStorage.updateHealth inc_Health .OnServerEvent:Connect(function(player,value) local char = player.Character local humanoid = char.Humanoid if value==0 then humanoid.MaxHealth = Humanoid.MaxHealth + 50 humanoid.Health = Humanoid.MaxHealth end end)