Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My sprint tool script is not working as intended?

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

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

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)

3 comments

0

This is the answer right there.

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())

0

I've updated my post accordingly to fit your requirements.

This discussion is preserved as it was. New answers, comments, and votes are closed.