Check If User Is Not Holding Key

I’m trying to make an acceleration system, but I don’t want it to interfere with my slide system. So, what I’m trying to do is making the acceleration script check if the user is not holding down Left Control. If not, they can accelerate. I’m not sure how to go about this, so I need some help. Here is my script.

local humanoid = script.Parent:WaitForChild("Humanoid")
local uis = game:GetService("UserInputService")

humanoid.Running:Connect(function(speed)
	if speed > 0 and game.Players.LocalPlayer.Character.Humanoid.WalkSpeed <2 then
		wait(1)
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed += 2
		wait(0.5)
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed += 2
		wait(0.5)
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed += 4
		wait(0.5)
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed += 4
	else
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
	end
end)


(Yep, it’s really bad scripting. I’m still very much a noob lol.)

2 Likes

Since you are still learning, here are some tips:

  • Spell out Roblox APIs and use PascalCase for them.
  • Define Roblox APIs at the top of your script.
  • Create variables for values and Instances that you will especially
    continually use.
  • Use task.wait() instead of wait().
  • Create functions for code that will be repeated, if reasonable (creating
    a function to add two values together is not reasonable, in most cases:
local function add(a: number, b: number)
    return a + b
end

print(add(5, 6)) -- Output: 11

It makes more sense to just type print(5 + 6), and even requires less characers.

Anyway, if you want to check if the player is not holding a key, you can use the UserInputService:IsKeyDown() method. I am assuming that you want to check if the player is holding the key or not if the speed is more than 0, so you’d write something like this:

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

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local INCREASE_DELAY = 0.5

local function addSpeed(amount: number)
    -- You don't have to repeat typing this somewhat long line.
    humanoid.WalkSpeed += amount
end

humanoid.Running:Connect(function(speed)
    if speed == 0 or humanoid.WalkSpeed > 2 then
        humanoid.WalkSpeed = 16
        return
    end

    if UserInputService:IsKeyDown(Enum.KeyCode.LeftControl) then
        return
    end

    task.wait(1)

    addSpeed(2)
    task.wait(INCREASE_DELAY)

    addSpeed(2)
    task.wait(INCREASE_DELAY)

    addSpeed(4)
    task.wait(INCREASE_DELAY)

    addSpeed(4)
end)

I don’t know why you are using the values 2 then 4, so I didn’t create a variable for that.

If the script is inside of StarterCharacterScripts, you can just access the character with script.Parent instead of Players.LocalPlayer.Character.

Also, if you want to gradually increase or decrease a value, you can use TweenService.

1 Like

Thank you so much! My script was quite repetitive, and these tips will be a great help!

You’re welcome. DRY (don’t repeat yourself) is one of the most important rules. As programmers, we have to try our best to be lazy.

1 Like

That’s a nice way to put it lol
Also, I think the acceleration isn’t working? The walkspeed seems to be the same, but there is no errors.

I believe that is because of this: humanoid.WalkSpeed > 2.

1 Like

Removing it seems to fix the problem, but leaves the acceleration uncapped. I tried

	if humanoid.WalkSpeed <28 then

but it just stopped the acceleration altogether. I got to the number 28 by adding the base walkspeed (16) and the walkspeed when fully accelerated.

I am going to go eat dinner. But before increasing* the speed, you can check if the WalkSpeed is more or equal to the cap:

if humanoid.WalkSpeed >= CAP then
    return
end

If this doesn’t work, maybe someone else can help while I am absent. Have a perfect day.

1 Like