Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

As X leaderstat increases, can player health increase with it?

Asked by 5 years ago

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!

2 answers

Log in to vote
1
Answered by
Mr_Unlucky 1085 Moderation Voter
5 years ago
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!

0
Wow thank you very much, it worked perfectly. And more importantly, thanks for helping me understand what you've done here as well, it's very much appreciated! :D topogigio 9 — 5y
0
only the local player will see the health change but not everyone else in the game User#23252 26 — 5y
0
yeah I should've realized that @Arsubai12 oops Mr_Unlucky 1085 — 5y
0
I've also realised that my script for leveling the player, as he reaches the needed XP is only client sided. So the level in the server's leaderstats is still at level 1. Should I try and do the same thing with a remote event, for increasing the player's level as well? topogigio 9 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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)

Answer this question