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
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