GUI buttons for all devices

So many games use GUI buttons to performs actions, mainly the GUI buttons are made under computer use and not any other device. So recently testing my game on mobile device I encountered some problems. When I press the GUI button with the following code it does nothing and I cant really find a article explaining how to do this.

script.Parent.Activated:Connect(function()
    script.Parent.Parent.Enabled = false
end)

For GUI Buttons, MouseButton1Clicked events and the suchlike are emulated onto mobile - in other words, these events will work across all devices even if they’re not clicked by a mouse.

Instead of using .Activated or MouseButton events, you can use either InputBegan or InputEnded. It’s more modern and diverse than Activated, and has multi-platform support as well (so you don’t have to use Touched and Activated)

This is an example script below that might help you

local Button = script.Parent -- as depicted by your code, i think.

-- InputBegan fires when you press or begin to hold a key
-- InputEnded fires when you release a key that you were holding.

Button.InputEnded:connect(function(Input, Core)
    if Input.UserInputType == Enum.UserInputType.Mouse or Input.UserInputType == Enum.UserInputType.Touch then
        script.Parent.Parent.Enabled = false
        -- additional code here
    end
end)

You can find additional information about it in the wiki.

1 Like

It works for PC but still mobile not

Testing this myself, it worked fine for me. Where is this button parented, and what type of script are you using for it? (i.e. normal Script, LocalScript, etc)

1 Like

Im using a local script and its parented to a textbutton

I tested this with both Studio’s emulator and a physical mobile device, and in both Instances I made it work. Is it erroring at all on Mobile? (you can use /console on mobile to check the developer console)

1 Like

nope theres no errors ill check if a image label blocks it or something

yes ok sorry the button works its just that image label was blocking it