function OnUnequip() script.Parent.Parent.Humanoid.WalkSpeed = 16 end
script.Parent.Unequipped:connect(OnUnequip)
I'm making a tool that when you equip it your walkspeed is set to 100
what I'm having trouble with is when you unequip it the walkspeed doesnt change back to 16
whats wrong with this script? I think it's because the tool is no longer the players child so it cant set the walkspeed to 16, but I don't know how to fix this
I'm a beginner scripter in need of help
When you unequip the tool, it will be placed back into your Backpack
. This is why 'script.Parent.Parent' will never have a Humanoid in it.
You have to reference the Character object, then reference the Humanoid.
1 | function OnUnequip() |
2 | local plr = script.Parent.Parent.Parent --script,tool,backpack,player |
3 | local hum = plr.Character.Humanoid; |
4 | hum.WalkSpeed = 16 ; |
5 | end |
6 |
7 | script.Parent.Unequipped:connect(OnUnequip) |
Also, you could just use a LocalScript.
01 | local players = game:GetService( 'Players' ) |
02 | local me = players.LocalPlayer |
03 | repeat wait() until me.Character |
04 | local char = me.Character |
05 | local humanoid = char:WaitForChild( "Humanoid" ) |
06 | local tool = script.Parent |
07 | -- If you aren't using a handle, make sure you untick the option in the tools properties |
08 |
09 |
10 | local minimum_walkspeed = 16 |
11 | local max_walkspeed = 100 |
12 |
13 |
14 | local function setWalkspeed(walkspeed) |
15 | humanoid.WalkSpeed = walkspeed |