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)
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
--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
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)