How to block running ⬇️

Hello, I have a question, how can I make the script that when I press CTRL stays blocked, for example, when I press CTRL the character runs but I have to keep it pressed, how can I only touch it without having to leave it pressed?

The Script:

local player = game.Players.LocalPlayer
local humanoid = player.Character.Humanoid
local runSpeed = 48 – Run Speed

– Function to make the character run
local function startRunning()
humanoid.WalkSpeed = runSpeed
end

– Function to stop the character from running
local function stopRunning()
humanoid.WalkSpeed = 16 – Normal Speed
end

– Event connections
game:GetService(“UserInputService”).InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftControl then
startRunning()
end
end)

game:GetService(“UserInputService”).InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftControl then
stopRunning()
end
end)

I don`t have musch experience in script.

1 Like

if you are talking about a toggle system, you could simply update a boolean to be NOT equal to itself every time an input begins
also, please make a variable for UIS. So that roblox will not have to get it every time

I have provided some EXAMPLE code below:

game:GetService(“UserInputService”).InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftControl then
running = not running
end
2 Likes

You could use RunService to solve you problem like so:

UserInputService.InputBegan:Connect(function(input)
RS.Heartbeat:Connect(function()
if input.KeyCode == Enum.Keycode.LeftControl then
-- Your code here
end
end)
end)
1 Like

He is asking for the opposite of this, I belive. Also, how is this any different from InputEnded?

1 Like

Oh, I must’ve misinterpreted the post, I usually just use InputBegan.

2 Likes

Ah, also, the Input object has events attached to it for when it ends/changes, so he could also use that as a solution. (if that was what he needed)

2 Likes

Definitely, I wasn’t really thinking of that.

2 Likes

Where in the script can I put it? Can you send me the script that I put with what you put added is that I don’t have that much exp.

1 Like

You would have to remove the whole inputended part and just use a variable. Once the key is pressed, set the variable to the opposite of itself, detailed in my post above, then check if the variable is equal to true, if it is, then make them run, else, make them stop running.

1 Like

This form? or other.

local player = game.Players.LocalPlayer
local humanoid = player.Character.Humanoid
local runSpeed = 48 – Run Speed

local function running()
humanoid.WalkSpeed = runSpeed
end

local function stopRunning()
humanoid.WalkSpeed = 16 – Normal Speed
end

game:GetService(“UserInputService”).InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftControl then
running = not running
end
end)

1 Like

local player = game.Players.LocalPlayer
local humanoid = player.Character.Humanoid
local runSpeed = 48 – Run Speed

– Function to make the character run
local function running()
humanoid.WalkSpeed = runSpeed
end

local function stopRunning()
humanoid.WalkSpeed = 16 – Normal Speed
end

game:GetService(“UserInputService”).InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftControl then
running = not running
if running == true then
running()
else
stopRunning()
end
end
end)
2 Likes

Thx for you help thank you very musch

2 Likes

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