For example, in one language I learned some of (I think Javascript?), there was a Map() function, which would take some inputs such as a range of numbers (say, 1 through 10), a target range (4 through 20), and a number from the first range (such as 9). It finds the number 9 in the first range, then maps it to the second range, so that it’s the same percentage of the total range through. So, the 9 in 1 through 10 would be mapped to a 18 in 4 through 20. Obviously, in this example, you could just multiply it by four, but that’s just to make the example easier on me. In a real scenario, you may be mapping from 1 through 10 to something like 3 through 6, which is more difficult and therefore needs a function. Does ROBLOX have something like this? I couldn’t find it when I looked.
3 Likes
-- Maps a given Range from a specific iterator to a new Range.
local function map(n, start, stop, newStart, newStop, withinBounds)
local value = ((n - start) / (stop - start)) * (newStop - newStart) + newStart
-- Returning basic value.
if not withinBounds then
return value
end
-- Returns Values constrained to exact Range.
if newStart < newStop then
return math.max(math.min(value, newStop), newStart)
else
return math.max(math.min(value, newStart), newStop)
end
end
for i = 0, 600 do
print(map(i, 0, 600, 0, 255, true))
-- Outputs Range 0-600 mapped between 0-255.
end
Or do you mean math.clamp?
4 Likes
I think your example doesn’t make sense, if I understand you correctly.
Going from a nine inside of 1-10 to 36 in 4-20 isn’t the same percentage through the range, since 36 in 4-20 is outside the range, while 9 in 1-10 is inside the range.
*It should be 18.22… The script above is correct. Sorry for causing confusion.
2 Likes
Yep, sorry I multiplied by four instead of 2 by accident.
1 Like
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.