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

Is there a way to use parameters in ContextActionService?

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)

1 comment

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

1 answer

✓ Accepted answer

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)

3 comments

0

Whens its like this, the p key still works in chat, but when its on 1 line, it doesn't

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.

0

oh sorry, it was a mistake i made with something else, my bad!

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