How to move canvas position properly by clicking a button

Hello Everyone!

I want to move the canvas position of a scrolling frame by the click of a button, and I need help.
I was able to make the canvas move properly, but the values are the same on different devices & it will move them to somewhere else on the canvas. I want to know how to fix this issue so it goes to the right area every time. It is a struggle and I would like to hear your thoughts about this. I listed my code this problem.

local passes = script.Parent.Store.Button1

local cash = script.Parent.Store.Button2

local perks = script.Parent.Store.Button3

local TweenService = game:GetService("TweenService")

local TweenSpeed2 = 0.09

local TweenSpeed = 0.09

local TweenFunction = TweenInfo.new(TweenSpeed, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0)

local TweenFunction2 = TweenInfo.new(TweenSpeed2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0)

passes.MouseEnter:connect(function()

script.Parent.Store.ScrollingFrame.CanvasPosition = Vector2.new(0, 300)
3 Likes

if you use gui you don’t need tween service

I’m not using TweenService. I’m talking specifically about this line of code: script.Parent.Store.ScrollingFrame.CanvasPosition = Vector2.new(0,0)

you don’t need vector2.new because it’s don’t make the canvas tween

you need to use
script.Parent.Store.ScrollingFrame.CanvasPosition:TweenPosition(udim2.new(-- put your position here --), enum.easingDirection.InOut, enum.easingStyle.linear, 0.5)

CanvasPosition and CanvasSize are both output in pixels, not scale. This means that if you have it sized according to scale, the canvasSize and therefore CanvasPosition are different on different screen sizes. You can use some math to figure out what the pixel position is based on the scale.

1 Like

What? You use a Vector2 to set the new CanvasPosition for the ScrollingFrame.

1 Like

Furthermore, 0,0 will always be the same, but 0,300 will be vastly different on a PC from on a phone. I would suggest that if your UI objects are positioned using scale, you change them from Vector2.new(0,300) to something like Vector2.new(0,CanvasPosition.Size.Y * [scale position here]).

My code may not be perfect, it’s been a while since I used scrollingFrames and I don’t have studio open to check that currently, I think that that or something similar to it should work.

6 Likes

I tried using that but kept showing an error, “TweenPosition is not a valid member of Vector2”. Is there anything else that could work?

1 Like

Is there a way where I can make it all one line of code? I’ve tried the math and it seems I have to write a line of code for every device. Could there be a more simpler way of doing this?

1 Like

Nevermind! I found a code that works. Thanks for your assistance. I truly appreciate it.

3 Likes

Sry for this reply, but i have the same problem. Could you send me the code pls, TYSM if you do it :smiley:

1 Like

This was a year ago. I don’t know if I have the code anymore

2 Likes

Ok no problem ty for the answer; i resolved with another solution

3 Likes