Hi! I'm making a game where you are a ball that can move and I'm trying to make it mobile compatiable. I'm kinda making a "d-pad" from the buttons... I'm using ContextActionService because I've heard that is the best way to make buttons for mobile, but there is a "bug" (It's probably not...) where if you hold down a button, move your finger away from the button and release, it will still move. Here's the code!
local A function aPressed(actionName, userInputState, inputObject) if userInputState == Enum.UserInputState.Begin then A = true elseif userInputState == Enum.UserInputState.End then A = false end end game.ContextActionService:BindAction("keyPressA", aPressed, true, Enum.KeyCode.A) game.ContextActionService:SetPosition("keyPressA", UDim2.new(-2, 0, 0.3, 0))
Which combination of buttons you press, will go through a "RunService" and change the players velocity.
Like everytime I make a question on this website, I found a solution... I tried printing out what states the UserInputState is returning to see what is wrong...
print(userInputState)
When you click, it will print out Enum.UserInputState.Begin. If you move while holding down the button, It will print out Enum.UserInputState.Change. If you stop holding down the button, it will print out Enum.UserInputState.End. BUT! If you hold down the button and move your finger away from the button and release! It will not print out Enum.UserInputState.End. I found a solution by doing this instead.
local aButton = game.ContextActionService:GetButton("keyPressA") aButton.MouseEnter:Connect(function() A = true end) aButton.MouseLeave:Connect(function() A = false end)
CAS:GetButton("Name of the button") will get the gui button from the playergui. Now you can do events that a image button can do. Two of those events are MouseEnter and MouseLeave. This is good to use if you want the player to be able to switch between buttons without taking their finger off the screen.