Health Based On Level

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

When the player levels up, his or her maximum health (MaxHealth) increases as well. The level value is in a folder called “DataFolder,” which is located in the player, and its descendant is a variable called “level,” where I’m trying to get my level value from. I will send a screenshot of question number 2. For example, if a player is at level 1 and the max health is at 100, when the player levels up, which is at level 2, the player’s max health is at 150 (just adding 50 health per level). I also want to see if you pro developers out there see what the problem is with the script I have created at question number 2.

  1. What is the issue? Include screenshots / videos if possible!

The script that I create doesn’t work.
The script I used In ServerscriptService:
image\

The data folder and the level value:
image

The error from Output:
image

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried to look up free models to help me in the studio tool since my browser couldn’t find a good solution to solve my problem. Sadly, it didn’t work either. At this point, it was like I kept getting into a dead end in a maze.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local player = game.Players.LocalPlayer
local datafolder = player.DataFolder
local level = datafolder.Level

game.Players.PlayerAdded:Connect(function(player)
	if level.Value == 3 then
		player.CharacterAdded:Connect(function(char)
			char.Humanoid.MaxHealth = 1000
		end)
	end
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

You can use a number progression to achieve this. Arithmetic progress would mean each level has exactly the same values away from each other. Geometric progression means each valley has a similar ratio to each other.

In both cases you can substitute n for level.

Formula:
a + d(n -1)
Where a is the base health and d is how much you want the health to go up by each level.
Looks like:

local function ArithmeticProgression (a, d, n)
    return d * (n - 1) + a
end

You can’t use game.Players.LocalPlayer on a server script. To access the player, you’d need a Players.PlayerAdded event like so:

local function getMaxHealthBasedOnLevel(level)
    return 100 + (level * 50) -- example
end

local function onPlayerAdded(player)
    local dataFolder = player:WaitForChild("DataFolder")
    local level = dataFolder:WaitForChild("Level")
    local function onCharacterAdded(character)
        local humanoid = character:WaitForChild("Humanoid")
        if humanoid then
            humanoid.MaxHealth = getMaxHealthBasedOnLevel(level.Value)
        end
    end
    if player.Character then
        onCharacterAdded(player.Character)
    end
    player.CharacterAdded:Connect(onCharacterAdded)
end

game.Players.PlayerAdded:Connect(onPlayerAdded)

Sadly It only adds 50 health, not per level however, I read your comment and I saw that I cant just use “game.Players.LocalPlayer` on a server script just to access the player” At least I have learned something from you <3

I’m interested in this. Can you give me an example of this? Please?

I saw you put an example in the code, my bad, so I’m going to remake it in my own code, BRB. It took me five minutes to notice it.

Okay I will make something simple then

Also I will do a geometric progression too if you are interested. Geometric progression increases by ratio and can be given by the formula ar^n

local BASE_HEALTH = 100 -- Starting level with base health
local HEATH_PER_LEVEL = 50 -- Health added per level (AP)
local RATIO_PER_LEVEL = 1.2 -- Health increases by 20% each level (GP)
local function ArithmeticProgression(a, d, n)
    return d * (n - 1) + a
end
local function GeometricProgression(a, r, n)
    return a * (r^n)
end
for level = 1, 10 do
    print("At level",level,"I have",ArithmeticProgression(BASE_HEALTH,HEATH_PER_LEVEL,level),"health.")
end
-- You can experiment with geometric progression if you like.

Just when you are using Geometric progression remember that r is the common ration. For Arithmetic progression d is the common difference.

1 Like

image
Good news! it goes up! Thank you all who helped me. <3