Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to make ContextActionService pass an argument?

Asked by 4 years ago
Edited 4 years ago

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.

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. Igoralexeymarengobr 365 — 4y
0
Nope doesn't work TheBigBro122 427 — 4y

2 answers

Log in to vote
0
Answered by
iuclds 720 Moderation Voter
4 years ago

-- 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
Ad
Log in to vote
-1
Answered by 4 years ago

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

Answer this question