Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make ContextActionService pass an argument?

I've been learning about ContextActionService and I need to pass to the function (In this case the binded function) some arguements

Here's the code

--LocalScript

local player = game.Players.LocalPlayer
local tool = script.Parent

local cas = game:GetService("ContextActionService")
local actionName = "openDoor"

function openDoor(slot) --This needs the slot variable
	slot.Value:WaitForChild("Open"):FireServer()
	tool:WaitForChild("Slot"):Destroy()
	slot.Value = nil
	
	cas:UnbindAction(actionName)
end

while true do
	local slot = tool:FindFirstChild("Slot")
	
	if slot and slot.Value ~= nil then
		cas:BindAction(actionName, openDoor(slot), false, "f") --IMPORTANT
	end
	
	wait(0.5)
end

also it gives me an error, Arguement 2 missing or nil: line 21

If you still don't understand, feel free to ask and I'll elaborate.

2 comments

0

I never searched on how ContextActionService works, buI believe that function openDoor(slot) should return some value, it should be why on line 21 argument 2 is nil.

0

Nope doesn't work

2 answers


-- open door function
function openDoor(slot,action,state,input) --This needs the slot variable
    slot.Value:WaitForChild("Open"):FireServer()
    tool:WaitForChild("Slot"):Destroy()
    slot.Value = nil

    cas:UnbindAction(actionName)
end

-- bind
cas:BindAction(actionName, function(action,state,input)
	openDoor(slot,action,state,input)
end, false, Enum.KeyCode.F) --IMPORTANT

Alright I fixed it... I just made the variable slot into a global variable instead of just being inside the while loop

so the whole code looks like this now

local player = game.Players.LocalPlayer
local tool = script.Parent

local cas = game:GetService("ContextActionService")
local actionName = "openDoor"

local slot --This is global now

function openDoor()
	slot.Value:WaitForChild("Open"):FireServer()
	tool:WaitForChild("Slot"):Destroy()
	slot = nil
	
	cas:UnbindAction(actionName)
end

while true do
 	slot = tool:FindFirstChild("Slot")
	
	if slot and slot.Value ~= nil then
		cas:BindAction(actionName, openDoor, false, "f")
	end
	
	wait(0.5)
end

This discussion is preserved as it was. New answers, comments, and votes are closed.