01 | health = 5 |
02 | active = false |
03 | walkSpd = 32 |
04 | script.Parent.Equipped:connect( function () |
05 | active = true |
06 | script.Parent.Parent.Humanoid.WalkSpeed = walkSpd |
07 |
08 | end ) |
09 |
10 | script.Parent.Unequipped:connect( function () |
11 | active = false |
12 | script.Parent.Parent.Humanoid.WalkSpeed = 16 --problem area |
13 |
14 | end ) |
15 |
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.
01 | local health = 5 |
02 | local active = false |
03 | local walkSpd = 32 |
04 | local player = game.Players.LocalPlayer |
05 |
06 | script.Parent.Equipped:Connect( function () |
07 | active = true |
08 | player.Character.Humanoid.WalkSpeed = walkSpd -- accessing the player's character |
09 | end ) |
10 |
11 | script.Parent.Unequipped:Connect( function () |
12 | active = false |
13 | player.Character.Humanoid.WalkSpeed = 16 |
14 | end ) |
15 |
16 | while active do -- you dont need to make a if statment |
17 | script.Parent.Parent.Humanoid.Health = script.Parent.Parent.Humanoid.Health + health |
18 | wait( 1 ) |
19 | 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
01 | Character = script.Parent.Parent |
02 | Player = game:GetService( "Players" ):WaitForChild(Character.Name) |
03 | -- Added Variables Above |
04 |
05 | health = 5 |
06 | active = false |
07 | walkSpd = 32 |
08 | script.Parent.Equipped:connect( function () |
09 | active = true |
10 | Character.Humanoid.WalkSpeed = walkSpd |
11 | end ) |
12 |
13 | script.Parent.Unequipped:connect( function () |
14 | active = false |
15 | Character .Humanoid.WalkSpeed = 16 --fixed Area :3 |