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

tool unequipped function doesnt work how do i fix?

Asked by 3 years ago
Edited 3 years ago
Tool = script.Parent
function onEquippedLocal()
    local humanoid = Tool.Parent:FindFirstChild("Humanoid")
    humanoid.MaxHealth = humanoid.MaxHealth + 25
    humanoid.Health = humanoid.MaxHealth
end

function onUnequippedLocal()
    local humanoid = Tool.Parent:FindFirstChild("Humanoid")
    humanoid.MaxHealth = humanoid.MaxHealth + 50
    humanoid.Health = humanoid.MaxHealth
end

Tool.Equipped:connect(onEquippedLocal)
Tool.Unequipped:connect(onUnequippedLocal)

basically when you equip your tool it gives you 125 maxhealth but when you unequip it doesnt make your maxhealth go up by 50

1 answer

Log in to vote
0
Answered by 3 years ago

When a player unequips a tool, it goes into the player's Backpack, it does not remain inside the character. So, inside your unequip function, Tool.Parent is actually the backpack, not the character.

Tool = script.Parent
function onEquippedLocal()
    local humanoid = Tool.Parent:FindFirstChild("Humanoid")
    humanoid.MaxHealth = humanoid.MaxHealth + 25
    humanoid.Health = humanoid.MaxHealth
end

function onUnequippedLocal()
    local Player = Tool.Parent.Parent
    local Character = Player.Character or Player.CharacterAdded:Wait()
    local Humanoid = Character:WaitForChild("Humanoid")
    Humanoid.MaxHealth = Humanoid.MaxHealth + 50
    Humanoid.Health = Humanoid.MaxHealth
end

-- Use 'Connect' as 'connect' is deprecated.
Tool.Equipped:Connect(onEquippedLocal)
Tool.Unequipped:Connect(onUnequippedLocal)

Ad

Answer this question