Positioning with Mobile Button problem

I’ve been trying to position my mobile button so that it is positioned the same in every device. Unfortunately this task proved difficulty to me and I want to know if there are any easier way to do this?

The script I’ve been trying to position the mobile button is from StarterCharacterScript btw

Could you provide the script? And for the position to be the same (relative to the screen’s size and dimensions) it would have to be scaled, offset is basically the distance from the anchor point.

Basically this:

local dashMobileButton
local mobileButtonPosition = UDim2.new(0.450, 0, 0.350, 0)
-- code 
if dashMobileButton then
	dashMobileButton.Position = mobileButtonPosition
end

you never declaring this? so its always nil

and whats the error if your getting one?

i defined it in the “–code” part, I thought that you would only need my code where I position the mobilegui

Sorry about that should’ve made it clear

do you have errors? and have you tried StarterPlayerScript since this is UI
or even placing the script in startgui folder

there is no error, and also if i ut it on the starteplayerscript it would error as the script is specifically designed to apply to the player’s character

seems like it should work as long as that UI button is there and setup right really depends on the rest of the code other than that

are you changing any of the other UI in this code?
and how are you referencing to the players ui

oh wait my bad i forgot to mention that my button is not a ui, it is made by context action service

sorry

and your sure your variable has the button referenced right maybe do a print on your dashMobileButton to make sure

I’m 100% sure it is referenced, the main problem is my positioning

What part of the positioning is wrong? Can you provide some images so we can get an understanding of the problem? Are you trying to position the button in the centre of the screen or smthn

sorry been a long night miss read the last part of that
you can position this a few ways and try to get it about the same its frustrating at time

you can use viewport size to get the exact pixels of the device and do an offset from that of say set amount or percentage of the pixels

you can also set the anchor points on the button to 1 then subtract from there if you are trying to align it based on the right bottom side of the screen using scale

and on ui you can get the absolute size also doing some math to get the percent say something like X * .10 for 10 percent of the actual size

1 Like

Ipad:


Samsung:

This issue is mentioned in this article under the adaptable UI section: Cross-Platform Design | Roblox Creator Documentation

The code snippet given in the article to position the button above the jump button

local Players = game:GetService("Players")

-- Get reference to player's jump button
local player = Players.LocalPlayer
local PlayerGui = player:WaitForChild("PlayerGui")
local ScreenGui = PlayerGui:WaitForChild("ScreenGui")
local TouchGui = PlayerGui:WaitForChild("TouchGui")
local TouchControlFrame = TouchGui:WaitForChild("TouchControlFrame")
local JumpButton = TouchControlFrame:WaitForChild("JumpButton")

-- Get absolute size and position of button
local absSizeX, absSizeY = JumpButton.AbsoluteSize.X, JumpButton.AbsoluteSize.Y
local absPositionX, absPositionY = JumpButton.AbsolutePosition.X, JumpButton.AbsolutePosition.Y

-- Create new button above jump button
local customButton = Instance.new("ImageButton")
customButton.Parent = ScreenGui
customButton.AnchorPoint = Vector2.new(0.5, 1)
customButton.Size = UDim2.new(0, absSizeX * 0.8, 0, absSizeY * 0.8)
customButton.Position = UDim2.new(0, absPositionX + (absSizeX / 2), 0, absPositionY - 20)

You can edit this script to achieve the position you want.

i have tried this before, but this only works if the script is in player scripts. Mine is in character script so it did not work

Might try something like this its similar to using the anchor and i think how they position the jump button etc on mobile

local mobileButtonPosition = UDim2.new(1, -120, 1, -120)

you may even use the jump buttons position as reference

The location of the script shouldn’t matter. The script never uses script.Parent. Maybe there was something wrong with your script? Can you show the code that you tried to use?