I need help finding the absolute canvas position of a textlabel or frame or anything that has a position.
In a scrolling frame, you have the canvas position where it determines the position you are in the frame. Problem is it is different for mobile and pc or different dimensions.
So now I am trying to figure out how to find the absolute canvas position.
Here’s the current code:
local Shortcuts = {}
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local localPlayer = Players.LocalPlayer :: Player
local MenuGui = localPlayer:FindFirstChild("PlayerGui"):FindFirstChild("Menu") :: ScreenGui
local Background = MenuGui:FindFirstChild("Background"):: ScrollingFrame
local SCROLL_TIME = 0.2
Shortcuts._shortcuts = MenuGui.Background.Introduction.Shortcuts:: Frame
Shortcuts._tweenScrollInfo = TweenInfo.new(SCROLL_TIME)
function Shortcuts._scrollDownToSection(Frame: Frame)
local canvasPosition --= Vector2.new(Background.AbsoluteWindowSize * Frame.Position)
local canvasTween = TweenService:Create(Background,Shortcuts._tweenScrollInfo,
{ CanvasPosition = canvasPosition }
)
local ScrollSound = Instance.new("Sound")
ScrollSound.SoundId = "rbxassetid://10066914500"
ScrollSound.Parent = game:GetService("SoundService")
ScrollSound:Play()
Debris:AddItem(ScrollSound,2)
canvasTween:Play()
end
return Shortcuts
He removed offset from framePosition.Y since the error states that you’re attempting to index the ‘offset’ of a ‘number’. However, framePosition.Y does have an offset, the one that does not is CanvasPosition. So in order to resolve the error it should instead be;
local Shortcuts = {}
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local localPlayer = Players.LocalPlayer
local MenuGui = localPlayer:FindFirstChild("PlayerGui"):FindFirstChild("Menu")
local Background = MenuGui:FindFirstChild("Background")
local SCROLL_TIME = 0.2
function Shortcuts._scrollDownToSection(Frame)
local framePosition = Frame.Position
local canvasPosition = Vector2.new(
UDim.new(0, 0), -- UDim for X-coordinate
UDim.new(0, math.max(0, framePosition.Y.Offset - Background.CanvasPosition.Y)) -- UDim for Y-coordinate
)
local canvasTween = TweenService:Create(Background, TweenInfo.new(SCROLL_TIME), {
CanvasPosition = canvasPosition
})
local ScrollSound = Instance.new("Sound")
ScrollSound.SoundId = "rbxassetid://10066914500"
ScrollSound.Parent = game:GetService("SoundService")
ScrollSound:Play()
Debris:AddItem(ScrollSound, 2)
canvasTween:Play()
end
return Shortcuts
I use absolute position here for the target position incase you are using a UIListLayout or UIGridLayout, which stop you from reading the UI’s position.
Final Code:
local Shortcuts = {}
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local localPlayer = Players.LocalPlayer
local MenuGui = localPlayer:FindFirstChild("PlayerGui"):FindFirstChild("Menu")
local Background = MenuGui:FindFirstChild("Background")
local SCROLL_TIME = 0.2
function Shortcuts._scrollDownToSection(Frame)
local framePosition = Frame.Position
local relativeAbsoluteOffset = framePosition.AbsolutePosition.Y - Background.AbsolutePosition.Y
local canvasPosition = Vector2.new(
Background.CanvasPosition.X,
Background.CanvasPosition.Y + relativeAbsoluteOffset
)
local canvasTween = TweenService:Create(Background, TweenInfo.new(SCROLL_TIME), {
CanvasPosition = canvasPosition
})
local ScrollSound = Instance.new("Sound")
ScrollSound.SoundId = "rbxassetid://10066914500"
ScrollSound.Parent = game:GetService("SoundService")
ScrollSound:Play()
Debris:AddItem(ScrollSound, 2)
canvasTween:Play()
end
return Shortcuts
This may just be me … but, I wish with questions like this the GUI setup would also be uploaded.
Guessing is not part of how most programmer’s like to do things …