Hello all,
I’ve created a staff for my game that grants you speed when you hold it, and takes away speed when you unequip it.
Here is the script:
–
local tool = script.Parent
function onEquip()
tool.Parent.Humanoid.WalkSpeed = 50
end
tool.Equipped:Connect(onEquip)
function onUnEquip()
print("aaa")
tool.Parent.Humanoid.WalkSpeed = 16
end
tool.Unequipped:Connect(onUnEquip)
–
The staff changes the players walkspeed, but when I unequip it, this error pops up (and my speed stays the same):
The print lines runs, so it isn’t a problem with the unequip function. If you need more details, don’t hesitate to ask.
Hope you all are enjoying your day,
–@Mithrandir6440
This is because the .Equipped event runs before the tool goes under the player’s character. You should use game.Players.LocalPlayer.Character
instead of tool.Parent
, and it is fine to put this in a local script.
1 Like
offizen
(offizen)
March 9, 2021, 3:38pm
#3
I think it’s because you are referring to the player and not the character which are both separate
Never mind just realised it now but try this
local player = game.Players.LocalPlayer
local tool = script.Parent
tool.Equipped:Connect(function()
player.Character.Humanoid.WalkSpeed = 50
tool.Unequippped:Connect(function()
player.Character.Humanoid.WalkSpeed = 16
end)
end)
You are asking for the Humanoid as a child of BackPack. You need another .Parent
in the code.
tool.Parent.Parent.Character.Humanoid.WalkSpeed = #
or the code that NotoriousNoah10 wrote. That should work too.
1 Like
You could simply do,
tool.Parent.Parent.Character.Humanoid.WalkSpeed = 16
This works because when a tool is unequipped, it gets parented to the player’s backpack and when it is equipped, it gets parented to the player’s Character, and Humanoid is a child of Character
1 Like
It worked! Thank you for solving this problem, and also thank to you @AAD232007 for clarifying on why this is