Ad
Log in to vote
1

Is there a way to use parameters in ContextActionService?

Asked by
CodeWon 121
10 days ago

I tried to make a script using context action service, but the function has a parameter, is there a way to get around this?

function openPhone(player) -- This is the function that will run
    local playerGui = player:WaitForChild("PlayerGui")

    local frame = playerGui.PhoneMenuGui.MainFrame

    frame:TweenPosition(
        UDim2.new(-0, 0, 0.262, 0), -- End position
        "Out", -- Easing direction
        "Bounce", -- Easing style
        1, -- Time in seconds
        false, -- Overide other tweens

        print("Phone opened"),
        chooseGradient(player)
    )

end

game.Players.PlayerAdded:Connect(function(player)
    local localPlayer = player
    if UserInputService.KeyboardEnabled then
        ContextActionService:BindAction("openAndTweenPhone", openPhone(localPlayer), false, "p")
    end
end)
0
https://developer.roblox.com/en-us/api-reference/function/ContextActionService/BindAction openPhone() will receive at least 3 parameters,try openPhone(a,b,c,d) print(a) print(b) print(c) print(d) end to see how it works TerranRecon 39 — 10d

1 answer

Log in to vote
1
Answered by
Speedmask 540 Moderation Voter
10 days ago
Edited 10 days ago

sure. in lua, you can always wrap it with an anonymous function when you get a parameter issue like this.

game.Players.PlayerAdded:Connect(function(player)
    local localPlayer = player
    if UserInputService.KeyboardEnabled then
        ContextActionService:BindAction(
        "openAndTweenPhone",
        function()
            openPhone(localPlayer)
        end,
        false,
        "p")
    end
end)

remember, all ContextActionService needs is a function. any function. doing it like this, it executes the function on the outside without parameters, but ends up running the function on the inside using parameters from outside :)

in fact, you're already doing it when you write game.Players.PlayerAdded:Connect(function(player) ... end)

0
Whens its like this, the p key still works in chat, but when its on 1 line, it doesn't CodeWon 121 — 10d
0
sorry, I am not sure what you mean by that. if you are talking about the binding parameters, the separate lines are just a stylistic choice so putting this on one line should be the same. the only thing I have changed is the function at line 6. Speedmask 540 — 10d
0
oh sorry, it was a mistake i made with something else, my bad! CodeWon 121 — 9d
Ad

Answer this question