Player cannot equip tool while running

Hello, so I wrote a script allowing players to run. But then, when the user holds LShift to run, they cannot use number keys to equip their tools.
What I have tried so far

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Humanoid.WalkSpeed = 6
		wait(0.1)
		Humanoid.WalkSpeed = 8
		wait(0.1)
		Humanoid.WalkSpeed = 10
		wait(0.1)
		Humanoid.WalkSpeed = 12
		wait(0.1)
		Humanoid.WalkSpeed = 14
		wait(0.1)
		Humanoid.WalkSpeed = 16
		wait(0.1)
		Humanoid.WalkSpeed = 18
		wait(0.1)
		Humanoid.WalkSpeed = 20
		wait(0.1)
	end
end)
UserInputService.InputEnded:Connect(function(input, gameProcessed)

	if input.KeyCode == Enum.KeyCode.LeftShift then
		Humanoid.WalkSpeed = 5
	end
end)

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	
	if gameProcessed then
		return
	end
end)

There is nothing in this code that would be causing this issue. In fact, it actually sounds like a studio bug. Have you playtested this code in a live-session rather than in studio testing?

Unrelated, but I highly recommend you use the TweenService rather than chaining wait statements to adjust the player walkspeed. Implementing it would look something like this:

local TweenService = game:GetService("TweenService")

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		TweenService:Create(humanoid, TweenInfo.new(1), { WalkSpeed = 20 })
	end
end)

And here’s the documentation for TweenService.

3 Likes

A lot of things could be done to make this much more efficient.
First of all, instead of using wait() a thousand times, simply use TweenService.

local TweenService = game:GetService("TweenService")

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		local tween = TweenService:Create(Humanoid,TweenInfo.new(1),{WalkSpeed = 20})
		tween:Play()
	end
end)

If you want the WalkSpeed to reset when the shift key is released, you could try this:

local tween
UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		tween = TweenService:Create(Humanoid,TweenInfo.new(1),{WalkSpeed = 20})
		tween:Play()
	end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if tween then
			tween:Cancel() -- if the player is sprinting, stop the tween.
		end
		Humanoid.WalkSpeed = 5 -- reset their WalkSpeed back to 5
	end
end)

--[[
this also prevents spam-clicking issues, 
since with the previous code if you spammed the shift key, 
multiple wait functions would be running at the same time
--]]

Since you didn’t give us your tool code I can’t tell you what the issue is, but this sprinting system should cause a lot less issues in the future.

3 Likes

First I just want to say thank you both for giving me more information about TweenService as I’m very new to Lua though my original problem has not been fixed

Also the tools are just basic tools with welded parts inside without any special modifications/scripts but I’m definitely trying out TweenService

2 Likes

I’m glad to have introduced you to TweenService, it is a really useful tool to use.

Just to make sure, do you have a part in your tool named “Handle” with all of your other parts welded to it? It may seem obvious but it’s a common mistake many new devs make. Also, make sure all your parts have Anchored and CanCollide set to false. If that’s already the case it’s probably either a problem on your end or a Roblox glitch. I can’t see why UserInputService would mess up tool equipping.

Yes all the tool has handles here is a video showing my problem (bad quality by the way).
robloxapp-20240404-2028539.wmv (1.9 MB)
I had to manually equip the items with my cursor while I was running, but I can use my number keys to equip when walking.

What does this part of the code do and is it really necessary? I doubt it really affects anything but maybe running two InputBegan functions at once messes up your sprinting system. Other than that I’m stumped.

1 Like

My idea was to cancel the previous InputBegan for player to equip their tools. Maybe it’s just studio problem but I cannot test it directly on Roblox right now as they are having problems where I live.

its to check if the input is valid and not coming from a textbox or chat, etc.

try setting your sprint to a different keybind like E and test if you can equip tools if you can then its a studio issue and it will work in game

I know that, but why have it in a completely different connection?

What do you mean he has 2 connections one for InputBegan and one for InputEnded

--one connection
UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Humanoid.WalkSpeed = 6
		wait(0.1)
		Humanoid.WalkSpeed = 8
		wait(0.1)
		Humanoid.WalkSpeed = 10
		wait(0.1)
		Humanoid.WalkSpeed = 12
		wait(0.1)
		Humanoid.WalkSpeed = 14
		wait(0.1)
		Humanoid.WalkSpeed = 16
		wait(0.1)
		Humanoid.WalkSpeed = 18
		wait(0.1)
		Humanoid.WalkSpeed = 20
		wait(0.1)
	end
end)

--inputended
UserInputService.InputEnded:Connect(function(input, gameProcessed)

	if input.KeyCode == Enum.KeyCode.LeftShift then
		Humanoid.WalkSpeed = 5
	end
end)

--a seperate InputBegan?
UserInputService.InputBegan:Connect(function(input, gameProcessed)
	
	if gameProcessed then
		return
	end
end)
1 Like

You just need to reverse the InputBegan setup.

I tested this script and I can equip/unequip tools while sprinting.


local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local UserInputService = game:GetService("UserInputService")
local ShiftKeyIsDown = false

UserInputService.InputBegan:Connect(function(input, gameProcessed)

	if gameProcessed then return end

	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			
			ShiftKeyIsDown = true

			repeat
				if ShiftKeyIsDown then
					Humanoid.WalkSpeed = Humanoid.WalkSpeed + 1
					task.wait()
				else
					break
				end
			until
			Humanoid.WalkSpeed == 200

		end
	end

end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)

	if gameProcessed then return end

	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.LeftShift then
			
			ShiftKeyIsDown = false

			repeat
				if ShiftKeyIsDown == false then
					Humanoid.WalkSpeed = Humanoid.WalkSpeed - 1
					task.wait()
				else
					break
				end
			until
			Humanoid.WalkSpeed == 16
		end
	end

end)

Here is the test file.

Sprint.rbxl (54.5 KB)

1 Like

Using your test file, I found out that key 1 and 2 will not work but key 3 4 and 5 work just fine in both studio and Roblox.
I guess there is something wrong with my studio but I have uninstalled studio many times and the problem still occurs.

That is very odd; I had not noticed an issue.

I re-tested and discovered that on my machine the 2 key will not equip if I am holding the LeftShift key.

I also noticed that the 2 key will not print a message if I am holding the W and LeftShift.

But, the 2 key will print a message if I am holding the LeftShit and any other key; just not the W.

Very strange.

Maybe somebody has an answer. Here is the file again with the test print message.

Sprint_TwoKey.rbxl (54.5 KB)

1 Like

This is a Roblox studio bug and should work in-game.

1 Like

Probably studio i know while testing something it would just go to the selection tool, move etc and wouldn’t equip anything

1 Like

I published the test file and played online.

In the live game, the 2 key will not equip a tool while holding the LeftShit if the W is also pressed.

But, the 2 key does equip a tool while holding the LeftShift if I am holding the A or S or D key; just not the W key.

1 Like

It seems like your issue might be related to the gameProcessed parameter in your InputBegan event. Try removing the check for gameProcessed to see if that resolves the problem.

1 Like