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)
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.