Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make when you press and hold CTRL that your character slows down?

Yep... I am bad at scripting so i don't have a script... :/ But can someone send me any script about how to do it or something? Thanks.

1 answer

Hey D3LETED_DATA,

First off, I'll start off by saying that this is not a request site, so we don't just give out scripts. However, I want to help you out with scripting but please, from now on try to attempt making your own scripts before consulting us. Anyways, I made the script that slows you down while you have CTRL pressed. Then when you let go of CTRL, it makes you go to normal speed. If you don't want the player to go back to normal speed after letting go of CTRL, you can just modify it and remove the last function. I have commented on the code to help you understand it more, so please just learn how the code works and next time maybe you can make your own!

##Anyways, here we go:

####Script

local plrs = game:GetService("Players"); -- Players Service
local plr = plrs.LocalPlayer; -- Local Player
local uis = game:GetService("UserInputService"); -- User Input Service
local pressing = false;  -- Debounce to decide if the character should be slowingdown or not.
local min = 3; -- Minimum amount of speed
local inc = .1; -- The amount the speed decreases by while CTRL is held. .1 is smooth slowing. 

-- Function to make the character slow -- 
uis.InputBegan:Connect(function(obj, gp) -- Anonymous function for clicking down.
	if obj.KeyCode == Enum.KeyCode.LeftControl and not gp then -- Checks if CTRL was pressed and checks if it was pressed while typing or not (Won't run if it was pressed while typing in chat.)
		pressing = true; -- Makes pressing true
		
		while pressing == true and wait() do -- While loop that only runs if pressing is true and it waits for 1/30th of a second.
			local char = plr.Character or plr.CharacterAdded:Wait(); -- The Character
			local hum = char:FindFirstChild("Humanoid"); -- The Humanoid 
	
			if hum and hum.WalkSpeed > min then -- Checks if the humanoid exists and the humanoid's speed is more than the minimum speed that it should have.
				hum.WalkSpeed = hum.WalkSpeed - inc; -- Sets the WalkSpeed to the walkspeed subtracted by the increment. It will eventually 
			end -- end for WalkSpeed checking if statement
		end -- end for while loop
	end -- end for if statement that checks what key you pressed and what condition you did so.
end) -- end for the anonymous function.


-- Function to make the character back to normal --
uis.InputEnded:Connect(function(obj, gp) -- Anonymous function for getting things back to normal.
	if obj.KeyCode == Enum.KeyCode.LeftControl and not gp then --Checks if the Key is CTRL that you stopped clicking.
		pressing = false; -- Makes pressing false so the loop that subtracts the speed stops.
		local char = plr.Character or plr.CharacterAdded:Wait(); -- Character
		local hum = char:FindFirstChild("Humanoid"); -- Humanoid of the Character
		hum.WalkSpeed = 16; -- Makes the WalkSpeed the normal speed.
	end -- end for the if statement to check if you pressed CTRL and the condition it was in.
end) -- end for function.

##Well, I hope I helped and I wish you a wonderful day.

~~ KingLoneCat

This discussion is preserved as it was. New answers, comments, and votes are closed.