How to use math.clamp with magnitude

basically, math.clamp with a maxDistance variable and your current magnitude:

local player = game:GetService("Players").LocalPlayer

local character = player.Character or player.CharacterAdded:Wait()

local sound = workspace.Main["around the clock glovey theme"]

local runService = game:GetService("RunService")

local MagnitudePart = workspace.Main
runService.RenderStepped:Connect(function()
local magnitude = (MagnitudePart.Position - character.Head.Position).Magnitude
-- this is where the math.clamp should be---sound.PlaybackSpeed = 10 / (MagnitudePart.Position - character.Head.Position).Magnitude --math.clamp(50, .5, 2)
if magnitude <=40 then
	if not sound.Playing then
		print("playing")
		sound:Play()
	end
elseif magnitude > 40 then
	if sound.Playing then
		print("not playing")
		sound:Stop()
		end
	end
end)

Wouldn’t math.clamp require 3 values though? Also I am a bit confused, couldn’t you set the Min/MaxDistance properties of the Sound instead?

Then again I don’t know what math.clamp does apart from returning a value depending on what number is higher/lower/ranged

1 Like

yeah, math.clamp does require 3 values and no if your talking about the RollOff.
im guessing this: math.Clamp(magnitude, lowest playback speed, highest playback speed)

Well you may need to set the PlaybackSpeed limits (Both Min and Max) so that it’ll return back the value you want it to stop at

Maybe something like this?

local player = game:GetService("Players").LocalPlayer

local character = player.Character or player.CharacterAdded:Wait()

local sound = workspace.Main["around the clock glovey theme"]

local runService = game:GetService("RunService")

local MagnitudePart = workspace.Main
runService.RenderStepped:Connect(function()
    local magnitude = (MagnitudePart.Position - character.Head.Position).Magnitude
    --New stuff--
    local MinPitch = 0.25
    local MaxPitch = 2
    local DistanceLimiter = magnitude / 10
    sound.PlaybackSpeed = math.clamp(DistanceLimiter, MinPitch, MaxPitch) --This will return back the MinPitch, Range, or the MaxPitch depending on what the Magnitude is
    if magnitude <=40 and not sound.Playing then
	    print("playing")
	    sound:Play()
    elseif magnitude > 40 and sound.Playing then
 	    print("not playing")
	    sound:Stop()
	end
end)

This is just my assumption and guess on how you could use math.clamp

1 Like

I’m at school, I’ll test this when I’m back, it’s seems like your correct though

1 Like

it works! yay math.clamp! but, (i swear i say this in every topic of mine) its opposite; the sound get slower the closer you are, when it should get faster the closer

on the side, could you give me a brief explanation on how distance limiter works?

clarifying this:

the sound playback speed is opposite; it should play faster when you get closer, instead its slower

Hm, I believe I mixed up the formula a bit lol

The DistanceLimiter prevents the magnitude from exceeding a huge number relevant to the pitch you’re wanting to play, which is why we divide it by 10 but it seems as though I mixed up

Try this:

local player = game:GetService("Players").LocalPlayer

local character = player.Character or player.CharacterAdded:Wait()

local sound = workspace.Main["around the clock glovey theme"]

local runService = game:GetService("RunService")

local MagnitudePart = workspace.Main
runService.RenderStepped:Connect(function()
    local magnitude = (MagnitudePart.Position - character.Head.Position).Magnitude
    --New stuff--
    local MinPitch = 0.25
    local MaxPitch = 2
    local DistanceLimiter = 10 / magnitude
    sound.PlaybackSpeed = math.clamp(DistanceLimiter, MinPitch, MaxPitch) --This will return back the MinPitch, Range, or the MaxPitch depending on what the Magnitude is
    if magnitude <=40 and not sound.Playing then
	    print("playing")
	    sound:Play()
    elseif magnitude > 40 and sound.Playing then
 	    print("not playing")
	    sound:Stop()
	end
end)
1 Like