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

My sprint tool script is not working as intended?

Asked by 3 years ago

So I scripted a tool that should make the player sprint once it is selected, and return the walkspeed back to normal once it is deselected. The player does sprint once they select the tool, but the problem is that when the player deselects the tool, the walkspeed doesn't return back to normal, and the sprinting continues... and I'm clueless as to why the deselect function isn't working. Any help would be appreciated. Thanks! (The script is below)

function onSelected(onSelected)
    script.Parent.Parent.Parent.Character.Humanoid.WalkSpeed = 100 
end

function onDeselected(onDeselected)
    script.Parent.Parent.Parent.Character.Humanoid.WalkSpeed = 16
end

script.Parent.Selected:connect(onSelected)
script.Parent.Deselected:connect(onDeselected)

2 answers

Log in to vote
0
Answered by 3 years ago

I'm not sure but maybe you spelt something wrong or deselected isn't the right function.

Ad
Log in to vote
0
Answered by
pwx 1581 Moderation Voter
3 years ago
Edited 3 years ago

Instead of Selected and Deselected, use Equipped and Unequipped.

local Tool = script.Parent
local Players = game:GetService('Players')

local Player = nil -- will obviously change upon equip

Tool.Equipped:Connect(function()
    Player = Players:GetPlayerFromCharacter(Tool.Parent)
    if Player.Character and Player.Character:FindFirstChild('Humanoid') then
        Player.Character.Humanoid.WalkSpeed = 100
    end
end)

Tool.Unequipped:Connect(function()
    -- added player check here incase the slight off chance it didn't find player
    if Player and Player.Character and Player.Character:FindFirstChild('Humanoid') then
        Player.Character.Humanoid.WalkSpeed = 16
    end
end)
0
This is the answer right there. Frometa1998 35 — 3y
0
I tried replacing this in the old script but it still didn't work (function Equipped() script.Parent.Parent.Parent.Character.Humanoid.WalkSpeed = 100 end function Unequipped() script.Parent.Parent.Parent.Character.Humanoid.WalkSpeed = 16 end script.Parent.Equipped:connect() script.Parent.Unequipped:connect()) XxJJ_p14yzxX 0 — 3y
0
I've updated my post accordingly to fit your requirements. pwx 1581 — 3y

Answer this question