I made a button and when its pressed then a function will play but I don’t wanna have 2 ifs deciding if player is mobile or pc and running same code but rather first detect if they are mobile and then run the code
You can do it with CAS (ContextActionService)
I’m pretty sure in function :BindAction() there’s a specific argument that is a boolean, when set to true, it’ll create a mobile button.
-- // Services \\ --
local ContextActionService = game:GetService("ContextActionService")
-- // Variables \\ --
local CreateMobileTouchButtons = true -- a boolean that :BindAction() takes in to register mobile buttons.
ContextActionService:BindAction("YourActionHere", function()
-- your code here!
end, CreateMobileTouchButtons, Enum.KeyCode.E)
Apparently, on the wiki, a ScreenGui and a Frame will be added to the PlayerGui. You can then customize this using ContextActionService:GetButton()
References:
This is my first support post and I hope I helped lol.
Does it matter what youractionhere string means and what does it do
In short, nope, “YourActionHere” is just for convenience to “bind” and “unbind” Contexts created by CAS.
It’s just like naming variables. You want to set it to something fitting such as for your case, “Interact” might be the action name.
Take a look at :BindAction() and see how it works:
And is there a way that I can set each button specific place?
Yes, there is a way.
Once you have a context created by CAS, use :GetButton() to get an ImageButton that was created for mobile players.
In this example here I create a context like my last example and get the button and assign the imageid property to a supposed asset.
-- // Services \\ --
local ContextActionService = game:GetService("ContextActionService")
-- // Variables \\ --
local CreateMobileTouchButtons = true -- a boolean that :BindAction() takes in to register mobile buttons.
ContextActionService:BindAction("YourActionHere", function()
-- your code here!
end, CreateMobileTouchButtons, Enum.KeyCode.E)
local MobileButton = ContextActionService:GetButton("YourActionHere") -- get the button with the actionName
MobileButton.ImageID = "Your asset id here."
MobileButton.Position = UDim2.new(0, 500, 0, 500)
Reference:
Edit: To match your need I also included in the example I provided of setting the position of the imagebutton