An issue with setting max health when you are a certain level

im struggling to understand why this isnt working, i have tried a numerous of different methods. I have tried a server sided script and have tried moving it into StarterPlayerScripts, neither worked.

the print functions were for testing, ignore those.

the code (local script, inside of StarterCharacterScripts):

game.Players.PlayerAdded:Connect(function(plr)
	local level = plr.levelInfo.level
	plr.CharacterAdded:Connect(function(char)
		if char.Humanoid then
			while wait() do
				if level.Value <= 0 then
					char.Humanoid.MaxHealth = 100
					char.Humanoid.Health = 100
					print("a")
				elseif level.Value <= 10 then
					char.Humanoid.MaxHealth = 150
					char.Humanoid.Health = 150
					print("b")
				elseif level.Value <= 30 then
					char.Humanoid.MaxHealth = 200
					char.Humanoid.Health = 200
					print("c")
				elseif level.Value <= 50 then
					char.Humanoid.MaxHealth = 300
					char.Humanoid.Health = 300
					print("d")
				elseif level.Value <= 100 then
					char.Humanoid.MaxHealth = 500
					char.Humanoid.Health = 500
					print("e")
				elseif level.Value <= 445 then
					char.Humanoid.MaxHealth = 700
					char.Humanoid.Health = 700
					print("f")
				elseif level.Value <= 990 then
					char.Humanoid.MaxHealth = 1000
					char.Humanoid.Health = 1000
					print("g")
				elseif level.Value <= 999 then
					char.Humanoid.MaxHealth = 1400
					char.Humanoid.Health = 1400
					print("h")
				end
			end
		end
	end)
end)

4 Likes

Are the if statements working with the prints inside?

1 Like

nope, which leads me to think it’s something with the level value. Im not sure tho

If the prints are not working the issue has to do with the level. What are you trying to accomplish? Higher levels have more HP?

yeah, im probably not doing it the best way either :sweat_smile:

I would only run the code whenever the level changes instead of every wait().

this should be in a server script inside ServerScriptService
and yes run the code inside of

level.Changed:Connect(function(lvl)


end)
2 Likes

the only thing about that is it wont update when you join the game

make it into a function and run the function when the player joins then put it inside of the Changed event as well

1 Like

Use Data Store Service to save the max health as a variable so it can be applied to the players humanoid when the player joined in

Can you show the data store script

As far as this goes i don’t see any problems but it could be the saving on leaving, So could you show us how you actually save the players level with the data store if you created one

here is the code for the level saving:

local dss = game:GetService("DataStoreService")
local levelStore =  dss:GetDataStore("PlayerLevel")

local function saveData(player)
	local tableToSave = {
		player.levelInfo.level.Value;
		player.levelInfo.exp.Value;
	}
	
	local success, err = pcall(function()
		levelStore:SetAsync(player.UserId, tableToSave)
	end)
end

game.Players.PlayerAdded:Connect(function(player)
	local levelInfo = Instance.new("Folder")
	levelInfo.Name = "levelInfo"
	levelInfo.Parent = player
	
	local lvl = Instance.new("NumberValue")
	lvl.Name = "level"
	lvl.Value = 0
	lvl.Parent = levelInfo
	
	
	local exp = Instance.new("NumberValue")
	exp.Name = "exp"
	exp.Value = 0
	exp.Parent = levelInfo
	
	local maxExp = player.levelInfo.level.Value * 100
	
	local data
	
	local success, err = pcall(function()
		data = levelStore:GetAsync(player.UserId)
	end)
	
	if success and data then
		lvl.Value = data[1]
		exp.Value = data[2]
	else
		print("player has no data")
		player.levelInfo.level.Value = 0
	end
	
	exp.Changed:Connect(function(newExp)
		if newExp <= maxExp then
			lvl.Value += 1
			exp.Value -= maxExp
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, err = pcall(function()
		saveData(player)
	end)
end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		local success, err = pcall(function()
			saveData(player)
		end)

		if success then
			print("saved2")
		else
			print("not saved2")
		end
	end
end)

In your code, you have a while wait() loop inside the CharacterAdded event, which is causing an infinite loop. You should remove the while wait() loop because it’s not necessary and could cause performance issues.

You’re calculating maxExp using player.levelInfo.level.Value * 100 when setting up the exp.Changed event. This should be calculated inside the event handler whenever the player’s level changes.

Try this out but it might not work correctly

local dss = game:GetService("DataStoreService")
local levelStore = dss:GetDataStore("PlayerLevel")

game.Players.PlayerAdded:Connect(function(player)
    local levelInfo = Instance.new("Folder")
    levelInfo.Name = "levelInfo"
    levelInfo.Parent = player

    local lvl = Instance.new("NumberValue")
    lvl.Name = "level"
    lvl.Value = 0
    lvl.Parent = levelInfo

    local exp = Instance.new("NumberValue")
    exp.Name = "exp"
    exp.Value = 0
    exp.Parent = levelInfo

    local function saveData()
        local tableToSave = {
            lvl.Value,
            exp.Value
        }
        
        local success, err = pcall(function()
            levelStore:SetAsync(player.UserId, tableToSave)
        end)
    end

    local data
    local success, err = pcall(function()
        data = levelStore:GetAsync(player.UserId)
    end)

    if success and data then
        lvl.Value = data[1]
        exp.Value = data[2]
    else
        print("player has no data")
    end

    lvl.Changed:Connect(function(newLevel)
        -- Update maxExp based on newLevel
        local maxExp = newLevel * 100
        if exp.Value >= maxExp then
            lvl.Value = newLevel + 1
            exp.Value = exp.Value - maxExp
            saveData()
        end
    end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    saveData(player)
end)

PlayerAdded doesn’t work in local scripts

Oh yea also make sure to use a regular script

it is in a server script now, thank you

the only thing about this is this part, i placed the function at the top so i could call it outside of PlayerAdded, so i can’t use it in PlayerRemoving

That’s ofc up to you.
But just to be sure do you mean like this?

local dss = game:GetService("DataStoreService")
local levelStore = dss:GetDataStore("PlayerLevel")

local function saveData(player)
    local levelInfo = player:FindFirstChild("levelInfo")
    if levelInfo then
        local lvl = levelInfo:FindFirstChild("level")
        local exp = levelInfo:FindFirstChild("exp")

        if lvl and exp then
            local tableToSave = {
                lvl.Value,
                exp.Value
            }

            local success, err = pcall(function()
                levelStore:SetAsync(player.UserId, tableToSave)
            end)
        end
    end
end

game.Players.PlayerAdded:Connect(function(player)
    --// Your Code
end)

game.Players.PlayerRemoving:Connect(function(player)
    saveData(player)
end)

yeah, everything looks fine on the level system. Well to me anyway. Im still having the same issue tho