Find distance in scale from left of frame to mouse

I’m trying to find the distance from the left of the timestamp to the players mouse

This is my current code, but it doesnt seem to work:

local function OffsetToScale(Offset)
local ViewPortSize = workspace.Camera.ViewportSize
return UDim2.fromScale(Offset.X / ViewPortSize.X, Offset.Y / ViewPortSize.Y)
end

local delta = OffsetToScale(Vector2.new(Mouse.X, Mouse.Y)) - (UDim2.fromScale(.25,1) - OffsetToScale(Vector2.new(ScrollerFrame.Size.X.Offset,0)) )

The delta should return a value 0-1 based on the distance

What am I doing wrong?

Here’s the properties for the frame:
image

I’m still in need of this, I’m not coming closer to a solution

local mouse = game.Players.LocalPlayer:GetMouse()
local runService = game:GetService("RunService")


local function getPosition()

	while true do
		print(script.Parent.Position-UDim2.new(0,mouse.Hit.p.X, 0,mouse.Hit.p.Y)) 
		runService.RenderStepped:Wait()
	end

end

getPosition()

¿You mean this?

No, I mean finding the distance between left of the frame and the players mouse

image
What I’m trying to do is make this timestamp scrollable, so I need to find the distance between mouse and the start of the frame so I know how far to put the progress.

I assume you want the function to return 0 when the position is at the left end of a Frame and 1 when it’s at the right end?
It’s not very clear because that’s what I see in

The delta should return a value 0-1 based on the distance

but your code seems to be using the Y axis of the mouse in some manner.

A GuiBase2D has AbsolutePosition and AbsoluteSize properties which correspond to its size in pixels and the position of its AnchorPoint on screen.

local frame -- any GUI element
local mousepos = Vector2.new(Mouse.X, Mouse.Y) -- sort of leaves me pondering where you get Mouse from? If it's a Vector2, then just use Mouse instead of recreating it

local topleft = frame.AbsolutePosition - (frame.AbsoluteSize * frame.AnchorPoint) -- position of the top left corner of the GUI element on the screen
local mouse01 = (mousepos - topleft) / frame.AbsoluteSize -- this will be a Vector2. If the mouse is at the top left corner of the GUI, then it will be (0, 0). If it's at the bottom right corner, it will be (1, 1).

-- pseudocode: resize a progress bar and write a timestamp
local progress = math.clamp(mouse01.X, 0, 1) -- not sure if the value to clamp is 1st or 3rd, did not check
progressbar.Size = UDim2.new(progress, 0, 1, 0)
progresstext.Text = secondsToTimestamp(progress * length) -- e.g. 2:34

I cannot test any of this code because I do not have Studio on this computer.

Edit: this also does not account for rotation of the frame, but that’s easy to compensate for and you most likely will not rotate your bar

Edit: for a second opinion, find any free model color picker that looks like this and inspect the code to see how it turns a mouse position into a number 0-1 within an ImageButton:
image

1 Like

Thank you for your help! I’m not too sure what’s wrong with your code but it caps at 1 half way through the frame and starts at 0.5

image

(It is printing the progress)
image


I forgot to respond with a solution yesterday night, but I ended up taking borrowing code from a free model and changing it a bit to work with my frame. The reason I used the code from the free model is because it also included a snap amount.

Here’s my finished code:

local snapAmount = 4 -- higher = snappier
			
local xOffset = math.floor((Mouse.X - ScrollerFrame.AbsolutePosition.X) / snapAmount + 0.5) * snapAmount
local xOffsetClamped = math.clamp(xOffset, 0, ScrollerFrame.AbsoluteSize.X)

local roundedAbsSize = math.floor(ScrollerFrame.AbsoluteSize.X / snapAmount + 0.5) * snapAmount
local roundedOffsetClamped = math.floor(xOffsetClamped / snapAmount + 0.5) * snapAmount

local sliderValue = roundedOffsetClamped / roundedAbsSize
			
ScrollerFrame.Progress.Size = UDim2.new(sliderValue,0,1,0)

Change your frame’s AnchorPoint to (0, 0) just to see how it behaves.
If that fixes it, then I (or you) mustn’t have accounted for AnchorPoint properly.

Don’t be ashamed, that’s what the Toolbox is there for.
Stigma against using free models has done a lot of damage to the game and the community.

1 Like

Yeah, I just realized that but I was trying to find a way to keep my anchor point how it was at (.5,.5).

Yes, I see that.

I forgot to actually read your code! You didn’t adjust the position based on AnchorPoint at all.

local xOffset = math.floor((Mouse.X - ScrollerFrame.AbsolutePosition.X + (ScrollerFrame.AbsoluteSize.X * ScrollerFrame.AnchorPoint.X)) / snapAmount + 0.5) * snapAmount
If it’s still not right, then turn the ... AbsolutePosition.X + ( ... into a ... AbsolutePosition.X - ( ...
(the plus into a minus)

1 Like

Yeah, I’m pretty much in the dark when it comes to scripting UI.


I decided to test your code with anchor point set to 0,0 and it does work perfectly, something with how you set up the anchor point must be wrong.

Anyways thanks!

1 Like