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