Advertisement
Mryeetmemes

SoundRandomizer script in new roblox studio template

May 15th, 2023
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.85 KB | None | 0 0
  1. local rng = Random.new()
  2.  
  3. local function RandomInRange(range: number)
  4.     return rng:NextNumber(-range / 2, range / 2)
  5. end
  6.  
  7. local function GetNumber(inst: Instance, name: string)
  8.     local total: number = 0
  9.     while inst do
  10.         total += inst:GetAttribute(name) or 0
  11.         inst = inst.Parent
  12.     end
  13.     return total
  14. end
  15.  
  16. local module = {
  17.     ["MakeOneshot"] = function(sound: Sound) -- pass in a prototype sound to be cloned
  18.         local pitchJitter : number = GetNumber(sound, "PitchJitter")
  19.         local volumeJitter : number = GetNumber(sound, "VolumeJitter")
  20.        
  21.         local clone = sound:Clone()
  22.         clone.Ended:Connect(function() clone:Destroy() end)
  23.         clone.Looped = false
  24.         clone.PlaybackSpeed = sound.PlaybackSpeed + RandomInRange(pitchJitter)
  25.         clone.Volume = sound.Volume + RandomInRange(volumeJitter)
  26.         clone.Parent = workspace
  27.        
  28.         return clone
  29.     end
  30. }
  31.  
  32. return module
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement