ContextActionService Mobile issue

Hello devs

So, I want to make a button for mobile players so that if they clicked it, the block gets dropped from their arms.

This is the code I wrote but it didn’t seem to work, it might look a bit messy but that’s off topic.

at first it didn’t seem to work so I used prints and that issue appeared


how can I solve that issue and prevent “Argument 2 missing or nill”?

any help appreciated thanks.

1 Like

It looks like onButtonPress is declared after line 27 which would explain why the debugger is saying it’s nil.

so whats the solution to make the script work?

Since you are declaring onButtonPress inside the function Block, you’ll either need to move onButtonPress outside of Block or execute Block before you use BindAction() so that onButtonPress has been declared and is accessible.

EDIT: Heres some examples of what I’m trying to say:

local ContextActionService = game:GetService("ContextActionService")

local function Block()
	
	function onButtonPressed()
		
		print("Test")
		
	end
	
end

 --This will error because onButtonPressed has not been defined
ContextActionService:BindAction("DropBlockButton", onButtonPressed, true, Enum.KeyCode.T)
local ContextActionService = game:GetService("ContextActionService")

local function Block()
	
	function onButtonPressed()
		
		print("Test")
		
	end
	
end

Block()

 --This works because Block() was executed and defined onButtonPressed
ContextActionService:BindAction("DropBlockButton", onButtonPressed, true, Enum.KeyCode.T)
local ContextActionService = game:GetService("ContextActionService")

local function Block()
	
end

function onButtonPressed()
	
	print("Test")
	
end

 --This works because onButtonPressed is declared outside Block()
ContextActionService:BindAction("DropBlockButton", onButtonPressed, true, Enum.KeyCode.T)

it worked but the block still doesnt get dropped from the players arm

but this one without context action service works and let the player drop the block from their arm

What does the RemoteEvent ClientBlock do?

so there should be a script in every block to let the other blocks to connect to both throw and take event and also fire them
image

Do all the ContextActionService stuff (Binding the action, creating the button)
inside the if - then statement of Block (After line 20) and create the function that will be used by ContextActionService:BindAction() BEFORE you bind the action but AFTER line 20. Then you can take the code from the UserInputService connected function and put it inside the binded function since ContextActionService will handle the input now.

sorry but uh… i didn’t understand

I tried to make the if - then that goes inside the function Block.

if CanGrabBlock == true then
	CanGrabBlock = false
	anim:Play()
	game.ReplicatedStorage.TakeBlock:FireServer(block)
	local function onButtonPressed()
		contextActionService:UnbindAction("DropBlockButton")
		game.ReplicatedStorage.TakeBlock:FireServer(block)
		anim:Stop()
		task.wait(1.5)
		CanGrabBlock = true
	end
	contextActionService:BindAction("DropBlockButton", onButtonPressed, true, Enum.KeyCode.T)
	contextActionService:SetTitle("DropBlockButton", "drop block")
	contextActionService:SetPosition("DropBlockButton", UDim2.new(0.292, 0, 0.087, 0))
end

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