health = 5 active = false walkSpd = 32 script.Parent.Equipped:connect(function() active = true script.Parent.Parent.Humanoid.WalkSpeed = walkSpd end) script.Parent.Unequipped:connect(function() active = false script.Parent.Parent.Humanoid.WalkSpeed = 16 --problem area end) while wait(1) do --ignore if active then script.Parent.Parent.Humanoid.Health = script.Parent.Parent.Humanoid.Health + health end end
It gives me "Humanoid is not a valid member of Backpack" at line 12. I can't figure out how to manipulate the humanoid of the player when the tool is uneqquiped.
Your getting the player's humanoid by going through the hierarchy, but it runs when the Unequipped
event fires. So when the Unequipped
event fires, the tool gets removed from the character which then you cant access the humanoid. You'd need to use LocalPlayer
to get the player's humanoid if this is a local script, as LocalPlayer is the player that the script is running on.
local health = 5 local active = false local walkSpd = 32 local player = game.Players.LocalPlayer script.Parent.Equipped:Connect(function() active = true player.Character.Humanoid.WalkSpeed = walkSpd -- accessing the player's character end) script.Parent.Unequipped:Connect(function() active = false player.Character.Humanoid.WalkSpeed = 16 end) while active do -- you dont need to make a if statment script.Parent.Parent.Humanoid.Health = script.Parent.Parent.Humanoid.Health + health wait(1) end
when you equip a tool, it will be added inside player's Character
but when you unequip the tool the parent of it will change to the player's backpack
so the Unequipped Event fires when the tool is already inside your backpack
Certainly this solution isn't the best, but it works
Character = script.Parent.Parent Player = game:GetService("Players"):WaitForChild(Character.Name) -- Added Variables Above health = 5 active = false walkSpd = 32 script.Parent.Equipped:connect(function() active = true Character.Humanoid.WalkSpeed = walkSpd end) script.Parent.Unequipped:connect(function() active = false Character .Humanoid.WalkSpeed = 16 --fixed Area :3 end) while wait(1) do --ignore if active then script.Parent.Parent.Humanoid.Health = script.Parent.Parent.Humanoid.Health + health end end