Run a script when 2 keys is pressed

Hello, I was wondering how I could make it so that instead of only having to press one key, you have to press 2 at a time, I want it for a slide, and that when you are pressing the W, A, S, or D key, and the C or RightControl, (So you are going forward and press the C or RightControl key for it to work) this is my script:



local UIS = game:GetService("UserInputService")
local char = script.Parent

local slideAnim = Instance.new("Animation")
slideAnim.AnimationId = "rbxassetid://16346371389"

local keybind = Enum.KeyCode.C
local canslide = true

UIS.InputBegan:Connect(function(input,gameprocessed)
	if gameprocessed then return end
	if not canslide then return end
	
	if input.KeyCode == keybind then
		canslide = false
		
		local playAnim = char.Humanoid:LoadAnimation(slideAnim)
		playAnim:Play()
		
		local slide = Instance.new("BodyVelocity")
		slide.MaxForce = Vector3.new(1,0,1) * 30000
		slide.Velocity = char.HumanoidRootPart.CFrame.lookVector * 100
		slide.Parent = char.HumanoidRootPart
		
		for count = 1, 8 do
			wait(0.1)
			slide.Velocity*= 0.7
		end
		playAnim:Stop()
		slide:Destroy()
		canslide = true
	end
end)

Thank you!

2 Likes
table of keys
local UIS = game:GetService("UserInputService")
local char = script.Parent

local slideAnim = Instance.new("Animation")
slideAnim.AnimationId = "rbxassetid://16346371389"

local keybinds = { -- List of key combinations
    {W = Enum.KeyCode.C},
    {W = Enum.KeyCode.RightControl},
    {A = Enum.KeyCode.C},
    {A = Enum.KeyCode.RightControl},
    {S = Enum.KeyCode.C},
    {S = Enum.KeyCode.RightControl},
    {D = Enum.KeyCode.C},
    {D = Enum.KeyCode.RightControl},
}

local canslide = true

UIS.InputBegan:Connect(function(input, gameprocessed)
    if gameprocessed or not canslide then
        return
    end

    for _, combo in ipairs(keybinds) do
        if input.KeyCode == Enum.KeyCode[combo[1]] and UIS:IsKeyDown(combo[2]) then
            canslide = false

            local playAnim = char.Humanoid:LoadAnimation(slideAnim)
            playAnim:Play()

            local slide = Instance.new("BodyVelocity")
            slide.MaxForce = Vector3.new(1, 0, 1) * 30000
            slide.Velocity = char.HumanoidRootPart.CFrame.lookVector * 100
            slide.Parent = char.HumanoidRootPart

            for count = 1, 8 do
                wait(0.1)
                slide.Velocity *= 0.7
            end
            playAnim:Stop()
            slide:Destroy()
            canslide = true

            break
        end
    end
end)

may need some modifications

2 Likes


The error line:

???

2 Likes

Hmm… 4nd try … Is this it?

Maybe
local UIS = game:GetService("UserInputService")
local char = script.Parent

local slideAnim = Instance.new("Animation")
slideAnim.AnimationId = "rbxassetid://16346371389"

local keybinds = { -- List of key combinations
	{"W", Enum.KeyCode.C},
	{"A", Enum.KeyCode.C},
	{"S", Enum.KeyCode.C},
	{"D", Enum.KeyCode.C},
	{"W", Enum.KeyCode.RightControl},
	{"A", Enum.KeyCode.RightControl},
	{"S", Enum.KeyCode.RightControl},
	{"D", Enum.KeyCode.RightControl},
}

local canslide = true

UIS.InputBegan:Connect(function(input, gameprocessed)
	if gameprocessed or not canslide then
		return
	end

	for _, combo in ipairs(keybinds) do
		local movementKey, actionKey = combo[1], combo[2]
		if actionKey and input.KeyCode == actionKey and UIS:IsKeyDown(Enum.KeyCode[movementKey]) then
			canslide = false

			local playAnim = char.Humanoid:LoadAnimation(slideAnim)
			playAnim:Play()

			local slide = Instance.new("BodyVelocity")
			slide.MaxForce = Vector3.new(1, 0, 1) * 30000
			slide.Velocity = char.HumanoidRootPart.CFrame.lookVector * 100
			slide.Parent = char.HumanoidRootPart

			for count = 1, 8 do
				wait(0.1)
				slide.Velocity *= 0.7
			end
			playAnim:Stop()
			slide:Destroy()
			canslide = true

			break
		end
	end
end)

I am getting a reaction off the key(s) with the added right ctrl,
however I do not have that anim … looks right to me otherwise.
Placed in StarterPlayer.StarterCharacterScripts as a LocalScript

2 Likes

same

2 Likes

There is a humanoid Move Direction property and humanoid.MoveDrection.Magnitude which checks the direction the humanoid is going in, you could also get the velocity of the humanoidrootpart to determine if the humanoid is moving, you could just run that when the slide key is pressed.

Humanoid | Documentation - Roblox Creator Hub

2 Likes

Nvm I fixed it using the

Humanoid.Running

Check it here: Humanoid | Documentation - Roblox Creator Hub

2 Likes

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