How to Increase Player Max Health with String Value

I want to make player health more than 100 when they having different ability
for example:
when player have stand called SP
the maxHealth goes to 350
when player have stand called Tw
MaxHealth goes to 375

local Player = game:GetService("Players").LocalPlayer

game.Players.PlayerAdded:Connect(function(Player)
	local Character = Player.Character
	local StandV = Character:FindFirstChild("Value"):WaitForChild("StandValue")
	local H = Character:WaitForChild("Humanoid")
	if H then
		if StandV.Value == "SP" then
			wait(2)
			H.MaxHealth = 350
		end
	end	
end)

the script didn’t change the max health

Go to StarterCharacterScripts and put try this.

script.Parent.Humanoid.Maxhealth = 100
script.Parent.Humanoid.Health = 100

(Change the numbers to whatever you want them to be)

2 Likes

This is so you can change all players health to whatever you want. Is this what you needed?

1 Like

Put this on StarterCharacterScripts


local char = script.Parent
local hum = char:WaitForChild'Humanoid'

local StandV = Character:FindFirstChild("Value"):WaitForChild("StandValue")

if StandV.Value == "SP" then
			wait(2)
			hum.MaxHealth = 350
            hum.Health = hum.MaxHealth
end

That will only work on Client. The humanoid’s health won’t change on Server.
So it’s still 100.

I see you’re using “game.Players.LocalPlayer” which is only defined in local scripts, this means that the change to MaxHealth will only reflect locally to the client (as opposed to it reflecting to the entire server for each client).
Use the following in a server script in the ServerScriptService folder:

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		if character:FindFirstChild("Value") then
			local val = character:FindFirstChild("Value")
			local standV = val:WaitForChild("StandValue")
			if standV.Value == "SP" then
				task.wait(1)
				humanoid.MaxHealth = 350
				humanoid.Health = 350
			end
		end
	end)
end)

No, i tested this method and works, on the client and sv has changed his current health

Infinite yield possible on 'Workspace.endsword_1.Characther:WaitForChild(“Humanoid”)

it says this on the output

for some reason it didn’t change the health

also sorry for late reply and late test

Why are you storing stats inside the player anyway? Store them inside the player instead either in leaderstats or any generic folder.

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		if player:FindFirstChild("Value") then
			local val = player:FindFirstChild("Value")
			local standV = val:WaitForChild("StandValue")
			if standV.Value == "SP" then
				task.wait(1)
				humanoid.MaxHealth = 350
				humanoid.Health = 350
			end
		end
	end)
end)

Use this and parent the stats to the player instance instead, this will work since I’ve tested both scripts myself but you shouldn’t store stats inside the character.

You mean like put it inside backpack?

I agree with Limited_Unique, store the stats outside the character because the character gets refreshed every time they die.

However if you really want to do it this way, you would need to check if the value exists inside the character, and then check if it has changed its property. This can be done like so:

-- make sure this is server side script:

local Players = game:GetService("Players")

local function updateHealth(character)
	local humanoid = character:WaitForChild("Humanoid")
	local SV = character:WaitForChild("Value"):WaitForChild("StandValue")
	local function checkSV()
		if SV.Value == "SP" then
			humanoid.MaxHealth = 350
			humanoid.Health = humanoid.MaxHealth
		end
	end
	checkSV()
	SV.Changed:Connect(checkSV)
end

Players.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	updateHealth(character)
	task.wait(1)
	player.CharacterAdded:Connect(updateHealth)
end)
2 Likes

Inside the player or inside a folder inside the player.

thanks for the help i dont know need check

why it dosen’t change the health when i add another value

		elseif SV.Value == "TW"	then
			humanoid.MaxHealth = 375
			humanoid.Health = humanoid.MaxHealth

It’s probably a problem with your structure. Could you send a picture of the explorer and/or the code that creates+edits the “StandValue” StringValue?

The other possible problem is that there might be an error with your syntax. Does your code look like this:

local Players = game:GetService("Players")

local function updateHealth(character)
	local humanoid = character:WaitForChild("Humanoid")
	local SV = character:WaitForChild("Value"):WaitForChild("StandValue")
	local function checkSV()
		if SV.Value == "SP" then
			humanoid.MaxHealth = 350
			humanoid.Health = humanoid.MaxHealth
		elseif SV.Value == "TW"	then
			humanoid.MaxHealth = 375
			humanoid.Health = humanoid.MaxHealth
		end
	end
	checkSV()
	SV.Changed:Connect(checkSV)
end

Players.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	updateHealth(character)
	task.wait(1) --Not needed btw. Doesn't break anything in almost all cases though.
	player.CharacterAdded:Connect(updateHealth)
end)


here the explorer

1 Like

oh wait it just me stupid put local script inside serverscriptservice

1 Like

I tested the code above. Here is a place file where that works:
DevForum SS 31.rbxl (32.7 KB)

That code works. The problem could be a few things:

  • The StringValue isn’t changing:
    • Check the code that changes the StringValue
  • The StringValue is being replaced:
    • Make sure you don’t delete the StringValue and replace it. The code above uses a reference to the Instance.

Oh okay great! Happy developing :grinning: