Please Help me fix this ContextActionService script

Please help me fix this script, I am new to ContextActionServeice so I don’t know what to do.


local camera = workspace.CurrentCamera
local plr = script.Parent
local root = plr:WaitForChild("HumanoidRootPart")
local uis = game:GetService("UserInputService")
local debounce = false
local activate = Instance.new("Sound", root)
local effect = Instance.new("ColorCorrectionEffect", game.Lighting)

local atm = game.Lighting.Atmosphere


effect.Brightness=0
effect.Contrast =0
effect.Saturation= 1
effect.TintColor=Color3.fromRGB(255, 238, 0)
effect.Enabled=false
activate.SoundId="http://roblox.com/asset/?id=9112789751"
activate.MaxDistance=150




local Context = game:GetService("ContextActionService")


local function Animode(Name,State,Input)


uis.InputBegan:Connect(function(began)
	if began.KeyCode == Enum.KeyCode.Space then  
		if not debounce then
			debounce = true
			
			game.Lighting.ColorCorrection.Enabled=not game.Lighting.ColorCorrection.Enabled
			
			activate:Play()
			
			game:GetService("TweenService"):Create(camera, TweenInfo.new(0.3, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 150}):Play()
			
			
			atm.Density = 0
			atm.Offset= 0
			activate:Play()
			activate.Looped=true
			activate.Volume=3
			activate.PlaybackSpeed =0.2
			activate.TimePosition= 0.5
			
			
			
			
			wait(4)
			game.Lighting.ColorCorrection.Enabled=not game.Lighting.ColorCorrection.Enabled
			
			
			
			game:GetService("TweenService"):Create(camera, TweenInfo.new(0.3, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 70}):Play()
			
			atm.Density = 0.4
			atm.Offset= 1
			debounce = false
			activate:Stop()
			
		end
	end
	end)
end








Context:BindAction("Attack",Animode,true,Enum.KeyCode.F)
Context:SetImage("Attack", "rbxassetid://10784697798")
Context:SetPosition("Attack", UDim2.new(0.1, 0,0.1, 0))

Remove the ‘UserInputService’ connection from the code.

local function Animode(Name,State,Input)
	if State == Enum.UserInputState.Begin and Input.KeyCode == Enum.KeyCode.Space then  
		--Code here.
	end
end

For some reason, the tasks inside the script are not working. Do I have to add something else inside this function?
local function Animode(Name,State,Input)

https://developer.roblox.com/en-us/api-reference/function/ContextActionService/BindAction
BindAction is thoroughly documented here.

Ok found the solution, the problem was we can not declare the key inside the context function. Otherwise, it will only detect the key we are declaring here:


And will ignore the key we are binding here:

Context:BindAction("ttac",Animode,true,Enum.KeyCode.Space)

Which will ignore the mobile button completely. and the mobile button won’t work.

Ok found the solution, the problem was we can not declare the key inside the context function.

This is false, the reason it didn’t work in your case is because you bound the action to one key and attempted to check if another key caused the action to occur. The following snippet works as intended.

local Game = game
local ContextActionService = Game:GetService("ContextActionService")

local function OnAction(ActionName, InputState, InputObject)
	if InputState == Enum.UserInputState.Begin and InputObject.KeyCode.Name == "Space" then
		print("Hello world!") --Prints when the 'Space' key is pressed.
	end
end

ContextActionService:BindAction("Action", OnAction, false, Enum.KeyCode.Space)

After looking at the remainder of the script I see that you were binding the action to the ‘F’ key (and a mobile button) but attempting to determine if the ‘Space’ key caused the action to occur. If you want to check if a key is being held use the IsKeyDown method of the ‘UserInputService’.
https://developer.roblox.com/en-us/api-reference/function/UserInputService/IsKeyDown

I will add that this isn’t a very mobile friendly approach as while the action has a mobile button a mobile device does not have a ‘Space’ key that can be readily pressed.

I think there is a misunderstanding

I changed the script’s binding to space in both cases right after you send me the fix and it was not working in my script case until I removed this.

And about this one:


This is exactly what you are missing. You cant do InputObject.KeyCode.Name == "Space" then. It will work Only on PC but not on mobile device

Try making the bind true here ContextActionService:BindAction("Action", OnAction, false, Enum.KeyCode.Space) in your new script to see the mobile button and test it on a mobile , it doesn’t work.

What you were proposing was a system where It works only on PC and what I wanted is both on PC and mobile with my custom button which is working now after I read this thread: ContextActionService:BindAction and found that none of the scripts is using Input.KeyCode ==

And about this one:

I don’t have any jump or other thing connected to the space button, all It will do is give the player a visual boost. So it will be fine I guess.

I have tested your new script in several ways and it only works on PC, I have modified it below and now it is working on mobile and Pc

local Game = game
local ContextActionService = Game:GetService("ContextActionService")

local function OnAction(ActionName, InputState, InputObject)
	if InputState == Enum.UserInputState.Begin then
		print("Hello world!") --Prints when the 'Space' key is pressed.
	end
end

ContextActionService:BindAction("Action", OnAction, true, Enum.KeyCode.Space)

This is exactly what you are missing. You cant do InputObject.KeyCode.Name == "Space" then . It will work Only on PC but not on mobile device

You can’t support mobile devices if you expect the multiple keys to be pressed/held in order for the action to occur that is why I didn’t think the action had a mobile button (until I read the remainder of the script). The ‘ContextActionService’ doesn’t support binding actions to multiple keys (which you were trying to do).

Try making the bind true here ContextActionService:BindAction("Action", OnAction, false, Enum.KeyCode.Space) in your new script to see the mobile button and test it on a mobile , it doesn’t work.

I never claimed that it would work, you’re misconstruing my replies.

I don’t have any jump or other thing connected to the space button, all It will do is give the player a visual boost. So it will be fine I guess.

If you want to support all platforms, actions (even visual ones) should be shared.

I have tested your new script in several ways and it only works on PC, I have modified it below and now it is working on mobile and Pc

Again, I never claimed that it would work for mobile devices, the issue now is that you’re not detecting if the ‘F’ key is being pressed/held (which you initially wanted to do).

local Game = game
local ContextActionService = Game:GetService("ContextActionService")
local UserInputService = Game:GetService("UserInputService")

local function OnAction(ActionName, InputState, InputObject)
	if InputState == Enum.UserInputState.Begin then
		if UserInputService.KeyboardEnabled and (not (UserInputService:IsKeyDown("F"))) then return end --Check if 'F' key is being pressed/held for keyboard enabled devices.
		--Do code.
	end
end

ContextActionService:BindAction("Action", OnAction, true, Enum.KeyCode.Space)

‘F’ and ‘Space’ in the above snippet can be swapped if necessary (one triggers the action while the other must be pressed/held for the action to occur).

1 Like

Ok, so that was on your mind?
I am no pro so I didn’t even think that you would think my script was not that much faulty, I just copy pasted some parts of the script from other feeds since I was totally new to Context and couldn’t find better threads to learn it, and since I had the feeling that the pattern of my script is almost identical to the right script I thought someone might help me to fix this broken script. Anyway, this is the thing that I wanted;

----Important: Put this script in StarterPlayer > StarterCharacterScripts
local camera = workspace.CurrentCamera
local plr = script.Parent
local root = plr:WaitForChild("HumanoidRootPart")
local uis = game:GetService("UserInputService")
local debounce = false
local activate = Instance.new("Sound", root)
local effect = Instance.new("ColorCorrectionEffect", game.Lighting)

local atm = game.Lighting.Atmosphere


effect.Brightness=0
effect.Contrast =0
effect.Saturation= 1
effect.TintColor=Color3.fromRGB(255, 238, 0)
effect.Enabled=false
activate.SoundId="http://roblox.com/asset/?id=9112789751"
activate.MaxDistance=150




local Context = game:GetService("ContextActionService")


local function Animode(Name,State,Input)

	if Name == "ttac" then


		if State == Enum.UserInputState.Begin then  

			if not debounce then
				debounce = true

				game.Lighting.ColorCorrection.Enabled=not game.Lighting.ColorCorrection.Enabled

				activate:Play()

				game:GetService("TweenService"):Create(camera, TweenInfo.new(0.3, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 150}):Play()


				atm.Density = 0
				atm.Offset= 0
				activate:Play()
				activate.Looped=true
				activate.Volume=3
				activate.PlaybackSpeed =0.2
				activate.TimePosition= 0.5




				wait(4)
				game.Lighting.ColorCorrection.Enabled=not game.Lighting.ColorCorrection.Enabled
				game:GetService("TweenService"):Create(camera, TweenInfo.new(0.3, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 70}):Play()

				atm.Density = 0.4
				atm.Offset= 1
				debounce = false
				activate:Stop()

			end
		end

	end
end








Context:BindAction("ttac",Animode,true,Enum.KeyCode.Space)
Context:SetImage("ttac", "rbxassetid://524302408")	
Context:SetPosition("ttac", UDim2.new(0.7, 0,-0.35, 0))