How would I add ContextActionService to my Blocking Script for my Fighting System?

How would I add CAS (ContextActionService) to this blocking script for my mobile fighters?

Not sure how I would add it, I’ve tried but I’m stuck at a roadblock.

I looked around on the devforum, other websites, and the create.roblox.com website and I still cannot figure it out.

local player =  game.Players.LocalPlayer
local uis =  game:GetService("UserInputService")

local blockingAnim =  script:WaitForChild("BlockingAnim")

local CD = 0.05

local debounce = false

local BlockingRemote =  game.ReplicatedStorage.BlockRemote.Blocking
local RemoveBlocking = game.ReplicatedStorage.BlockRemote.RemoveBlocking

local char = player.Character or player.CharacterAdded:Wait()

local hum = char:WaitForChild("Humanoid")

local gaurdingAnimation =  hum:LoadAnimation(blockingAnim)



uis.InputBegan:Connect(function(input,Typing)
    if not Typing then
        if input.KeyCode == Enum.KeyCode.F and debounce == false then
            if char:FindFirstChild("BlockBreak") == nil  and char:FindFirstChild("Stun") == nil  and char:FindFirstChild("eStun") == nil then
                debounce = true
                BlockingRemote:FireServer()
                game.TweenService:Create(workspace.CurrentCamera,TweenInfo.new(.3,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0),{FieldOfView = 65}):Play()
                hum.WalkSpeed = 6
                gaurdingAnimation:Play()
                wait(CD)
                debounce = false

            end

        end
    end
end)

uis.InputEnded:Connect(function(input,Typing)
    if input.KeyCode == Enum.KeyCode.F then
        RemoveBlocking:FireServer()
        game.TweenService:Create(workspace.CurrentCamera,TweenInfo.new(.3,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0),{FieldOfView = 70}):Play()
        gaurdingAnimation:Stop()

        hum.WalkSpeed = 8
    end
end)

RemoveBlocking.OnClientEvent:Connect(function()
    RemoveBlocking:FireServer()
    game.TweenService:Create(workspace.CurrentCamera,TweenInfo.new(.3,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0),{FieldOfView = 70}):Play()
    gaurdingAnimation:Stop()

    hum.WalkSpeed = 8
end) 
1 Like

So, the ContextActionService can register keybinds and also add mobile buttons.
You can register input like this:

local blockKey = Enum.Keycode.T
local contextActionService = game:GetService("ContextActionService")

contextActilService:BindAction("Block", function(actionName, inputObject)
    if actionName == "Block" then
        print("BLOCKING!")
    end
end, true, Enum.Keycode.T)

The BindAction method takes three parameters. The name of the bind, the function to execute when it is called, if you would like a mobile button, and a keycode if you would like it to input with a keycode as well.

I hope this helps you out a bit.
https://create.roblox.com/docs/reference/engine/classes/ContextActionService#BindAction
To unbind the action, you can simply call UnbindAction(“Block”).

This didn’t help me at all. I’m trying to make it where mobile players can hold a button to block.

If you want to see it more thoroughly add me on discord. tyedev1#8281

That code creates a context action service button on mobile as well as having a keybind for PC. You don’t have to have the keybind.

Still, I don’t understand what you want me to do with this code you sent me. I’ve made it into a local script and it doesn’t work.

Mind showing me your current script?