Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

can I map input values to an output range?

Asked by 4 years ago
Edited 4 years ago

In the context of an music amplitude bar, how would I go about mapping my values that currently go from 0 to 1, into a different output range, say 5 to 10, or even 10 to 1. If this is possible let me know.

I have put the code below. Any advice is greatly appreciated!

--wait for Sound
local Sound = game.workspace:WaitForChild("Sound") 
local Players = game:GetService("Players")

-- wait for local player PlayerGui
local LocalPlayer = Players.LocalPlayer
local playerGui = LocalPlayer:WaitForChild("PlayerGui")

-- create a ScreenGui
local screenGui = Instance.new("ScreenGui", playerGui)

-- create a holder for our bar
local frame = Instance.new("Frame", screenGui)
frame.AnchorPoint = Vector2.new(0.5,0.5)
frame.Position = UDim2.new(0.5, 0, 0.5, 0)
frame.Size = UDim2.new(1, 0, 0.05, 0)

-- create a bar
local bar = Instance.new("Frame", frame)
bar.Position = UDim2.new(0, 0, 0, 0)
bar.Size = UDim2.new(1, 0, 1, 0)
bar.BackgroundColor3 = Color3.new(0, 1, 0)
local Baseplate = workspace.Baseplate

-- define a max loudness
local maxLoudness = 1000

-- animate the amplitude bar
while true do
    local amplitude = math.clamp(Sound.PlaybackLoudness / maxLoudness, 0, 1)
    bar.Size = UDim2.new(amplitude, 0, 1, 0)
    bar.BackgroundColor3= Color3.new(amplitude, 0 , 0.1)
    Baseplate.Transparency = amplitude
    print(amplitude)
    wait()
end

2 answers

Log in to vote
0
Answered by
RayCurse 1518 Moderation Voter
4 years ago
Edited 4 years ago

Assuming that you want the distribution of the numbers to stay the same, you can convert the range from 0 to 1 to any other range with addition, multiplication, and negation.

local amplitude1 = amplitude * 2 -- 0 to 2
local amplitude2 = amplitude * 5 + 5 -- 5 to 10
local amplitude3 = -amplitude * 9 + 10 -- 10 to 1
0
Looks promising! if this works I will accept your answer. alivemaeonman 14 — 4y
0
your method works, but I have found a universal formula for this answer. alivemaeonman 14 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

I have found a formula that does what this question entails.

https://math.stackexchange.com/questions/377169/going-from-a-value-inside-1-1-to-a-value-in-another-range/377174#377174

Answer this question