How do I Increase/Decrease a player's health when a tool is equipped?

	wait(0.5)
	print("Sledgehammer equipped")
	script.Parent.Humanoid.Health = 75
	script.Parent.Humanoid.MaxHealth = 75
--This will change the Health of the player when equipping this tool.

I don’t have good scripting knowledge so this is a very bad script. is there a way to do the effect mentioned in the title?

Add a Local script inside of the Tool and paste this script, It should work I’ve tested it.

local Tool = script.Parent
local Player = game.Players.LocalPlayer

Tool.Equipped:Connect(function()
Player.Character.Humanoid.MaxHealth = 75
Player.Character.Humanoid.Health= 75
end)
Tool.Unequipped:Connect(function()
Player.Character.Humanoid.MaxHealth = 100
Player.Character.Humanoid.Health = 100
end)

Doing so, would only affect the player and the server won’t see those changes.

Either use a Server Script, or use a Remote Event.

1 Like
local Players = game:GetService("Players")
local Player = script.Parent:FindFirstAncestorWhichIsA("Player")
local Humanoid = Player.Character:WaitForChild("Humanoid")

local Tool = script.Parent

Tool.Equipped:Connect(function()
	Humanoid.MaxHealth = 75
	Humanoid.Health = 75
end)

Tool.Unequipped:Connect(function()
	Humanoid.MaxHealth = 100
	Humanoid.Health = 100
end)

image

1 Like

I would like to add to this. Because the above script will heal the player, if the player’s health is below the maxhealth.

local Players = game:GetService("Players")
local Player = script.Parent:FindFirstAncestorWhichIsA("Player")
local Humanoid = Player.Character:WaitForChild("Humanoid")

local EquipHealth = 75
local UnequipHealth = 100

local Tool = script.Parent

Tool.Equipped:Connect(function()
	Humanoid.MaxHealth = EquipHealth
	if Humanoid.Health > EquipHealth then
		Humanoid.Health = EquipHealth
	end
end)

Tool.Unequipped:Connect(function()
	Humanoid.MaxHealth = UnequipHealth
end)
1 Like

Explanation can be found in the reply below.

Or FindFirstChildWhichIsA

But indeed FindFirstAncestorWhichIsA is wrongly used here.

WhichIsA for OfClass because the former takes into account child classes whereas the latter does not. No classes are derived from the ‘Player’ class.

I wouldn’t look more into the topic. But a player shouldn’t be found either way.