Tool unequip not resetting player's speed

So i have a tool that multiplies player speed by 25%. But when i unequip it, It does not set back to the regular speed. Any way to fix this?
Code

local amt = 5
script.Parent.Equipped:Connect(function(mouse)
	if script.Parent.Parent:FindFirstChild("Humanoid")~=nil then
		script.Parent.Parent:FindFirstChild("Humanoid").WalkSpeed*=amt
	end
end)
function Unequipped()
	if script.Parent.Parent:FindFirstChild("Humanoid")~=nil then
		script.Parent.Parent:FindFirstChild("Humanoid").WalkSpeed/=amt
	end
end
script.Parent.Unequipped:Connect(Unequipped)

It multiplies the speed by 500% though. Also consider setting the humanoid to a variable. I don’t know why it’s not working otherwise.

Well the amt is for good testing in studio. That way i know if it actually sets the speed back to regular or not

script.Parent.Equipped:Connect(function(mouse)
	if script.Parent.Parent:FindFirstChild("Humanoid")~=nil then
		script.Parent.Parent:FindFirstChild("Humanoid").WalkSpeed = 80
	end
end)

script.Parent.Unequipped:Connect(function()
	if script.Parent.Parent:FindFirstChild("Humanoid")~=nil then
		script.Parent.Parent:FindFirstChild("Humanoid").WalkSpeed = 16 -- 16 x 5 = 80
	end
end)

One big issue… I have a 2x speed gamepass with this. So this won’t work if the user has 2x speed. Because it just sets it to the roblox default. And not 32 (when user has 2x speed)

here are some changes

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local GamepassId = 69420 -- replace 69420 with the gamepass Id
local Humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
local player

script.Parent.Equipped:Connect(function(mouse)
	if humanoid ~= nil then
		player = Players:GetPlayerFromCharacter(humanoid.Parent)
		humanoid.WalkSpeed = 80
	end
end)

script.Parent.Unequipped:Connect(function()
	if humanoid ~= nil then
		if MarketplaceService:UserOwnsGamePassAsync(player.UserId,  GamepassId) then
			humanoid.WalkSpeed = 32
		else
			humanoid.WalkSpeed = 16
	end
end)

The script itself has to multiply and divide the speed so it can work without wasting lines. And there is no set speed, The amt is how much it will multiplty/divide

why do you want to multiply…

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local gamepassId = 69420 -- replace 69420 with the gamepass Id
local humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
local player

local amt = 5

script.Parent.Equipped:Connect(function(mouse)
	if humanoid ~= nil then
		player = Players:GetPlayerFromCharacter(humanoid.Parent)
		humanoid.WalkSpeed *= amt
	end
end)

script.Parent.Unequipped:Connect(function()
	if humanoid ~= nil then
		if MarketplaceService:UserOwnsGamePassAsync(player.UserId,  gamepassId) then
			humanoid.WalkSpeed *= 2
		else
			humanoid.WalkSpeed /= amt
	end
end)

So that way, It will increase speed no matter how much speed the user currently has.

Now i am getting Attempt to index nil with walkspeed.

its beacuse when the player unequips the tool it will be parented to players backpack so

script.Parent.Unequipped:Connect(function()

local character = workspace:FindFirstChild(script.Parent.Parent.Parent.Name)
character.Humanoid.WalkSpeed = 16

end)

I already fixed the issue with it…

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.