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
workspace:WaitForChild(Player.Name)
local Character = Player.Character
local Humanoid = Character.Humanoid
script.Parent.Equipped:Connect(function()
Humanoid.MaxHealth = 200
Humanoid.Health = 200
end)
script.Parent.Unequipped:Connect(function()
Humanoid.MaxHealth = 100
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
3 comments
can you put the text in the regular format? its a pain to look at
ok fine ill try
done