How can i convert this Vector3 to a Udim2 ? [Script]

  1. What do you want to achieve? Hello, what i would like to make is when the player touches the screen of his phone with his finger, then a GUI appears where he touches the screen. I’ve searched the forum but I couldn’t find any case similar to mine, and the solutions I’ve found don’t fit my script.I don’t know how to convert the finger position with a frame position. Thanks

  2. What solutions have you tried so far?

userInputService.TouchStarted:Connect(function(inPutObject1, gameProcessed)
	print("POSITION1    "..tostring(inPutObject1.Position))
	local Position1 = inPutObject1.Position
	
	local relativePosition = Position1 - plr.PlayerGui.ScreenGui.Deplacement.AbsolutePosition
	plr.PlayerGui.ScreenGui.Deplacement.Position = UDim2.fromOffset(relativePosition.X, relativePosition.Y)
	plr.PlayerGui.ScreenGui.Deplacement.Visible = true
end)

The position of the touchscreen is a Vector3 which is similar in form to a Vector2 (Z is always 0). Here is the error message that appears with this script :

Capturez

I’ve added more information so I’m bringing up this answer

Update: I managed to get the script to work but unfortunatly the GUI doesn’t position itself in the center, it’s still a little off to the left. I try to find a solution

userInputService.TouchStarted:Connect(function(inPutObject1, gameProcessed)
	print("POSITION1    "..tostring(inPutObject1.Position))
	local Position1 = inPutObject1.Position
	
	plr.PlayerGui.ScreenGui.Deplacement.Position = UDim2.fromOffset(Position1.X, Position1.Y)
	plr.PlayerGui.ScreenGui.Deplacement.Visible = true

Update 2: Just put anchorpoint 0.5,0.5 to the frame and it will always be anchored in the center. Here is the solution

1 Like

Glad you found your answer, its seems to really work well.
image

1 Like
1 Like

I tried your script it doesn’t seem to be well adapted for mobile players, the frame doesn’t appear in the right places, but it doesn’t matter my script above does the job

Quite easy to port to mobile devices.

local tweens = game:GetService("TweenService")
local userInput = game:GetService("UserInputService")
local players = game:GetService("Players")
local player = players.LocalPlayer

local clickFrame = script:WaitForChild("ClickFrame")

local screenGui = script.Parent

local _tweens = table.create(8)
local debounce = false

local function onInputBegan(inputObject, gameProcessed)
	if inputObject.UserInputType.Name ~= "MouseButton1" and inputObject.UserInputType.Name ~= "Touch" then return end
	if gameProcessed then return end
	if debounce then return end
	debounce = true
	local clickFrameClone = clickFrame:Clone()
	clickFrameClone.Position = UDim2.new(0, inputObject.Position.X, 0, inputObject.Position.Y)
	clickFrameClone.Parent = screenGui
	table.insert(_tweens, tweens:Create(clickFrameClone.Down, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {Position = UDim2.new(0.5, 0, 0.8, 0)}))
	table.insert(_tweens, tweens:Create(clickFrameClone.DownLeft, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {Position = UDim2.new(0.2, 0, 0.8, 0)}))
	table.insert(_tweens, tweens:Create(clickFrameClone.DownRight, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {Position = UDim2.new(0.8, 0, 0.8, 0)}))
	table.insert(_tweens, tweens:Create(clickFrameClone.Left, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {Position = UDim2.new(0.2, 0, 0.5, 0)}))
	table.insert(_tweens, tweens:Create(clickFrameClone.Right, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {Position = UDim2.new(0.8, 0, 0.5, 0)}))
	table.insert(_tweens, tweens:Create(clickFrameClone.Up, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {Position = UDim2.new(0.5, 0, 0.2, 0)}))
	table.insert(_tweens, tweens:Create(clickFrameClone.UpLeft, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {Position = UDim2.new(0.2, 0, 0.2, 0)}))
	table.insert(_tweens, tweens:Create(clickFrameClone.UpRight, TweenInfo.new(0.5, Enum.EasingStyle.Linear), {Position = UDim2.new(0.8, 0, 0.2, 0)}))
	for _, tween in ipairs(_tweens) do
		tween:Play()
	end
	table.clear(_tweens)
	task.wait(0.5)
	clickFrameClone:Destroy()
	debounce = false
end

userInput.InputBegan:Connect(onInputBegan)

test.rbxl (34.1 KB)
https://gyazo.com/c4f07527efa396cb320c61883fdc7026

1 Like

Mark your post as solved, so people don’t keep trying to solve it, when you already got it working.

1 Like