Need help with adding mobile support trough "ContextActionService"

Once I tried to make mobile support while learning “ContextActionService”, I got stuck on one issue when it comes to script making it. I am stuck on point “how to make it without rewriting entire script”.

Code is right here : (it is a cut one, but it will be enough for me)

local userinputservice = game:GetService("UserInputService")
local CAS = game:GetService("ContextActionService")
CAS:BindAction("Def", onKeyPress, true, Enum.KeyCode.Q)
CAS:SetTitle("Def", "Def")
CAS:SetPosition("Def", UDim2.new(0.125,0,0.73,0))

function onKeyPress(inputObject, gameProcessedEvent)
	if script.Parent.CanDoAnything.Value == true and CanUseAnything == true and inChat == false then
		local Charr = localPlayer.Character
		if Charr ~= nil then
			if Charr.Statuses:FindFirstChild("IsStunned") == nil and Charr.Statuses:FindFirstChild("Silence") == nil and Charr:FindFirstChild("IsDying") == nil then
				if inputObject.KeyCode == Enum.KeyCode.Q or inputObject.KeyCode == Enum.KeyCode.ButtonB then
					if inChat == false and script.Parent.CurrentCooldowns.Defence.Value == 0 and Charr.CurrentRES.Value >= script.Parent.Costs.Defence.Value then
						CanCombo = false
						CanUseAnything = false
						script.Parent.CurrentCooldowns.Defence.Value = script.Parent.Cooldowns.Defence.Value
						game.ReplicatedStorage.SkillEvents.DefenceEvent:FireServer(localPlayer.leaderstats.Class.Value,localPlayer.Character)
						wait(script.Parent.Pauses.Defence.Value)
						CanCombo = true
						CanUseAnything = true
					end
				end
			end
		end
	end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

I read trough the documenation about CAS multiple times, but as a result I got lost into the stuff. I tested stuff trough “prints” - it stops on the line “if inputObject.KeyCode == Enum.KeyCode.Q or inputObject.KeyCode == Enum.KeyCode.ButtonB then”
(other values like Combo and e.t.c. are not important, and I tested them - entire script works fine on laptop)

Some help or suggestion would be highly appreciated. This entire code is located in one LocalScript in player’s backpack.

Your passed parameters are not correct, check this script that is from the Roblox documentation:

local function handleAction(actionName, inputState, _inputObject)
	if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
		print("Reloading!")
	end
end

ACTION_RELOAD is a name of the action btw in ur case it’s called “Def”

Okay, so if I have that CAS button in my script called “Def”, then in the script that you sent “actionName” is equal to “Def”, and “_inputObject” is “Enum.KeyCode.Q”

Is this right?

No need to pass the key, it’s already binded in the BindAction line.

U js need to detect when the inputstate is equal to begin

local userinputservice = game:GetService("UserInputService")
local CAS = game:GetService("ContextActionService")
CAS:BindAction("Def", onKeyPress, true, Enum.KeyCode.Q)
CAS:SetTitle("Def", "Def")
CAS:SetPosition("Def", UDim2.new(0.125,0,0.73,0))

function onKeyPress(actionName, inputState, InputObject)
	if script.Parent.CanDoAnything.Value == true and CanUseAnything == true and inChat == false then
		local Charr = localPlayer.Character
		if Charr ~= nil then
			if Charr.Statuses:FindFirstChild("IsStunned") == nil and Charr.Statuses:FindFirstChild("Silence") == nil and Charr:FindFirstChild("IsDying") == nil then
				if inputState == Enum.UserInputState.Begin then
					if inChat == false and script.Parent.CurrentCooldowns.Defence.Value == 0 and Charr.CurrentRES.Value >= script.Parent.Costs.Defence.Value then
						CanCombo = false
						CanUseAnything = false
						script.Parent.CurrentCooldowns.Defence.Value = script.Parent.Cooldowns.Defence.Value
						game.ReplicatedStorage.SkillEvents.DefenceEvent:FireServer(localPlayer.leaderstats.Class.Value,localPlayer.Character)
						wait(script.Parent.Pauses.Defence.Value)
						CanCombo = true
						CanUseAnything = true
					end
				end
			end
		end
	end
end

--game:GetService("UserInputService").InputBegan:connect(onKeyPress)

Here i rewrote your script.

I tried using this script, now it simply doesn’t work, nor the button appears on the screen in mobile phone testing.

local Combo = 1
local CurrentCombo = 1
local CanCombo = true
local CanTrulyCombo = true
local CanUseAnything = true
local userinputservice = game:GetService("UserInputService")
local inChat = false

local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer -- from a LocalScript
local mouse = localPlayer:GetMouse()
local CAS = game:GetService("ContextActionService")

userinputservice.TextBoxFocused:connect(function()
	inChat = true
end)
userinputservice.TextBoxFocusReleased:connect(function()
	inChat = false
end)

local userinputservice = game:GetService("UserInputService")
local CAS = game:GetService("ContextActionService")
CAS:BindAction("Def", onKeyPress, true, Enum.KeyCode.Q)
CAS:SetTitle("Def", "Def")
CAS:SetPosition("Def", UDim2.new(0.075,0,-0.1,0))

function onKeyPress(actionName, inputState, InputObject)
	if script.Parent.CanDoAnything.Value == true and CanUseAnything == true and inChat == false then
		local Charr = localPlayer.Character
		if Charr ~= nil then
			if Charr.Statuses:FindFirstChild("IsStunned") == nil and Charr.Statuses:FindFirstChild("Silence") == nil and Charr:FindFirstChild("IsDying") == nil then
				if inputState == Enum.UserInputState.Begin then
					if inChat == false and script.Parent.CurrentCooldowns.Defence.Value == 0 and Charr.CurrentRES.Value >= script.Parent.Costs.Defence.Value then
						CanCombo = false
						CanUseAnything = false
						script.Parent.CurrentCooldowns.Defence.Value = script.Parent.Cooldowns.Defence.Value
						game.ReplicatedStorage.SkillEvents.DefenceEvent:FireServer(localPlayer.leaderstats.Class.Value,localPlayer.Character)
						wait(script.Parent.Pauses.Defence.Value)
						CanCombo = true
						CanUseAnything = true
					end
				end
			end
		end
	end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

This is the entire local script that is put into backpack, and it should be somewhere in this red zone, but button does not show up.

You connected the function again for the User input Service, and i just gave you an idea to where start from.

Okay, I will figure something out later. Thanks for the help!

1 Like

No Problem, the idea i gave you will work already, im js writing the script on phone, so there might be a syntax error

Nope, it was all good from your side, I just had to put “CAS” stuff where I add the bind AFTER the function - everything functions very well now, once again - thanks for your support!

1 Like

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