How to place a gui frame on a value between a line?

I made a clone of a gui frame but I need it to be placed on a line based on a number, the problem is the beginning and end of the number line changes sometime. This is what I want

This is what I have so far
newKeyframe.Position = UDim2.new((1 / (layers:FindFirstChild(selectedObject, true).ObjectVal.Value.End.Value - layers:FindFirstChild(selectedObject, true).ObjectVal.Value.Start.Value)) * keyframe.Frame.Value - 0.015, 0, 0, 0)

Thanks for the help!

2 Likes

You can use linear interpolation (lerp) to achieve this,
how it works:

linear interpolation uses 3 values, the start of the line, the end of the line and the percentage.
then, via a formula using these three, it returns a point on the line, which is at the given percentage.

here is the formula:

Start + (End - Start) * Percentage

so to get your newKeyframe on line with bounds ObjectVal.Value.End and ObjectVal.Value.Start we can do:

local ObjectValue = layers:FindFirstChild(selectedObject, true).ObjectVal;
--doesn't ffc accept a string only? well i don't care how you get your bounds,
--its about calculating the position anyway...

local start = ObjectValue.Value.Start.Value;--assuming these are vector3 or 2
local endd = ObjectValue.Value.End.Value;
local percentage = 0.5 --or 0.15, 0.75, any value from 0 (for start) to 1(for end)

local targetPos = start + (endd - start) * percentage;
newKeyframe.Position = Udim2.fromOffset(targetPos.X, targetPos.Y);

and there, linear interpolation will place your ui on the line, based on the percentage

1 Like

End and start are just a number, I only need the x axis to be placed

oh well, uhh then I think it is the same formula:

--the formula is for 2d stuff, but it actually works on 3d too, so
--we can assume it works on 1d

--[Same script]
newKeyframe.Position = Udim2.fromOffset(targetPos, 0) --since target is just a number

if you don’t know for sure if 1d works, you can create Vector2’s with X to start or end, and Y to 0

1 Like

You can normalize the value on the given range to get the relative scale coordinate. I broke it into several variables for clarity.

local range = layers:FindFirstChild(selectedObject, true).ObjectVal.Value
local min = range.Start.Value
local max = range.End.Value
local value = keyframe.Frame.Value
local x = (value - min) / (max - min) -- Normalize to [0, 1]
newKeyframe.AnchorPoint = Vector2.new(0.5, 0.5) -- Move from center
newKeyframe.Position = UDim2.fromScale(x, 0.5)
2 Likes

hey, mind if you recreate this on roblox studio and pass the .rblx or rbxm file?

1 Like

hey, mind if you recreate this on roblox studio and pass the .rblx or rbxm file?

This text will be blurred

1 Like

I actually made an example while writing the answer. Just throw it into StarterGui and read the script instructions to test via command line.

RangeExample.rbxm (7.4 KB)

Okay, the op and me are both looking for the same answer, so, thank you so much!

1 Like

Thanks a lot! This really helped me.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.