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

How do i keep the health the same after someone dies?

Well i want to keep the health the same it's the very bottom function i feel like i have to change something i this function can you tell me what? Sorry i didn't word correctly i meant like the health changes everytime they l evel up but then when they die it goes back to 100

game.Players.PlayerAdded:connect(function(player)
	stat = Instance.new("IntValue", player)
	stat.Name = "leaderstats"
	
	Gold = Instance.new("IntValue", stat)
	Gold.Name = "Gold"
	
	Exp = Instance.new("IntValue", stat)
	Exp.Name = "XP"
	
	Level = Instance.new("IntValue", stat)
	Level.Name = "Level"
	Level.Value = 0
	

	xpTotal = 11 * Level.Value
	
	player.leaderstats.XP.Changed:connect(function()
		
		
		if Exp.Value > 11 * Level.Value then
		Level.Value = Level.Value + 1 --add a level
		Exp.Value = Exp.Value - xpTotal --If its greater than five carry over
		
		end
		if Exp.Value == 10 * Level.Value then
			Level.Value = Level.Value + 1 -- add a level
			Exp.Value = 0 --if it = 4 then Send exp to zero
	end
	end)
	player.leaderstats.Level.Changed:connect(function()
			playa = player.Character or player.CharacterAdded:wait()
			playa.Humanoid.MaxHealth = Level.Value * 10 + 100
			playa.Humanoid.Health = playa.Humanoid.MaxHealth
	end)
end)

2 comments

0

We need more detail. What about it are you trying to keep the same. What are you trying to do, what is your code doing, what do you think it should do, why do you think it should do something. This is all info you should at least try to provide..

0

Poorly worded question, gave very little details. -1 rep.

2 answers

You need to do that in CharacterAdded. Enter this between Lines 35 and 36:

    player.CharacterAdded:connect(function(character)
        wait() --Not 100% sure of how temperamental Roblox is when a character respawns. Just in case.
        character:WaitForChild("Humanoid").MaxHealth = level.Value * 10 + 100
        character.Humanoid.Health = character.Humanoid.MaxHealth
    end)

Like the other answers have shown, we must set the now hp when the characters spawns. CharacterAdded

Here is a quick example:-


-- create instances quickly
local function mkIns(insType, par, name)
	local tmp = Instance.new(insType, par)
	tmp.Name = name
	return tmp
end

game.Players.PlayerAdded:connect(function(plr)
	local stat = mkIns("IntValue", plr, 'leaderstats')
	
   	mkIns('IntValue', stat, 'Gold')
	local xp = mkIns('IntValue', stat, 'XP')
	local level = mkIns('IntValue', stat, 'Level')
	
	-- this will be 0 ??? so why are you doing this
    --xpTotal = 11 * Level.Value
	
		-- connect the event to the object we do not need to go to the player we have the object use it
	 xp.Changed:connect(function(newValue)
		if newValue > 11 * level.Value then
        	level.Value = level.Value + 1 --add a level
        	xp.Value = xp.Value - (11 * level.Value) 
		elseif xp.Value == 10 * level.Value then
            level.Value = level.Value + 1 -- add a level
            xp.Value = 0 --if it = 4 then Send exp to zero
        end
	end)
	
	-- add the changed event to the level but we could also do this within the xp changed as we know when the plr levels up
	level.Changed:connect(function(newValue)
		
		-- update plr hp
		local plrModel = game.Workspace:FindFirstChild(plr.Name)
		if plrModel then
			local hum = plrModel:FindFirstChild('Humanoid')
			if hum then
				hum.MaxHealth = level.Value * 10 + 100
				hum.Health = level.Value * 10 + 100
			end
		end
	end)
	
	-- gives the player the hp when they join the game
	plr.CharacterAdded:connect(function(plrModel)
		local hum = plrModel:WaitForChild('Humanoid') -- wait for the humanoid
		hum.MaxHealth = level.Value * 10 + 100
		hum.Health = level.Value * 10 + 100
	end)
end)

This is only an example, please comment if you do not understand how / why this code works but first check the wiki.

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