Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I change health from unequiping a tool?

Asked by 3 years ago
Edited 3 years ago
script.Parent.Equipped:Connect(function()
    local Humanoid = script.Parent.Parent.Humanoid
    script.Parent.Parent.Humanoid.MaxHealth = 200
    script.Parent.Parent.Humanoid.Health = 200
end)
script.Parent.Unequipped:Connect(function()
    local Humanoid = script.Parent.Parent.Humanoid
    script.Parent.Parent.Humanoid.MaxHealth = 100
    script.Parent.Parent.Humanoid.Health = 100
end)

When I un-equip it, it thinks that I mean the Player.Backpack, but I want to get the humanoid. It works for the first one

0
can you put the text in the regular format? its a pain to look at likejagar 17 — 3y
0
ok fine ill try likejagar 17 — 3y
0
done collycoolguyguy111 0 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

Hello,

The reason you are encountering this issue is because when a tool is equipped, it is placed within the player's character. When a tool is unequipped, it is placed within the player's backpack.

Because you did not use code blocks or format your original post, I've taken the time to format your original code:

script.Parent.Equipped:Connect(function() 
    local Humanoid = script.Parent.Parent.Humanoid 
    script.Parent.Parent.Humanoid.MaxHealth = 200 
    script.Parent.Parent.Humanoid.Health = 200 
end) 

script.Parent.Unequipped:Connect(function() 
    local Humanoid = script.Parent.Parent.Humanoid 
    script.Parent.Parent.Humanoid.MaxHealth = 100 
    script.Parent.Parent.Humanoid.Health = 100 
end) 

We can see in your code, that when the Unequipped function is called you attempt to set the humanoid variable as the tool's parent... Which is the player's backpack.

To fix this issue, there are a few things I would recommend:

Assign the Localplayer's player object, character, and humanoid at runtime. This way you don't need to keep assigning the player's humanoid every time you pull in/out the weapon. To do this we would add the following to your code:

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

workspace:WaitForChild(Player.Name) --wait for local player's character to be loaded

local Character = Player.Character -- assign player's character
local Humanoid = Character.Humanoid --assign humanoid from character

script.Parent.Equipped:Connect(function() 
    Humanoid.MaxHealth = 200 --change function to use new variable
    Humanoid.Health = 200 
end) 

script.Parent.Unequipped:Connect(function() 
    Humanoid.MaxHealth = 100 --change function to use new variable
    Humanoid.Health = 100 
end) 

This solution fixes your issue by making sure the humanoid is referenced properly AND allows you to clean up your code.

Hope this answer helped you!

Cheers,

Chase

Ad

Answer this question