Mryeetmemes

SoundPlayer script in new roblox studio template

May 15th, 2023
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.13 KB | None | 0 0
  1. local Randomizer = require(script.SoundRandomizer)
  2. local rng = Random.new()
  3.  
  4. local function GatherSounds(inst: Instance)
  5.     local list = {}
  6.     if inst:IsA("Sound") then
  7.         table.insert(list, inst)
  8.     end
  9.    
  10.     for _, i in pairs(inst:GetDescendants()) do
  11.         if i:IsA("Sound") then
  12.             table.insert(list, i)
  13.         end
  14.     end
  15.     return list
  16. end
  17.  
  18. local function PickFrom(inst: Instance) : Sound?
  19.     local sounds = GatherSounds(inst)
  20.     if #sounds == 0 then return nil end
  21.     return sounds[rng:NextInteger(1, #sounds)]
  22. end
  23.  
  24. type Positionable = Part | Attachment | Vector3 | Vector2
  25. type Vec = Vector2 | Vector3
  26.  
  27. local function ToVector3(at: Vector2) : Vector3
  28.     return workspace.Camera:ScreenPointToRay(at.X, at.Y, 50).Origin
  29. end
  30.  
  31. local function MakeTempPart(at: Vec) : Part
  32.     local part = Instance.new("Part")
  33.     part.CanQuery = false
  34.     part.CanTouch = false
  35.     part.CanCollide = false
  36.     part.Transparency = 1
  37.     part.Size = Vector3.new(1, 1, 1)
  38.     part.Anchored = true
  39.    
  40.     if typeof(at) == "Vector3" then
  41.         part.Position = at
  42.     else
  43.         part.Position = ToVector3(at)
  44.     end
  45.    
  46.     part.Parent = workspace
  47.     return part
  48. end
  49.  
  50. local function CreateOneShot(prototype: Instance, from: Positionable?) : Sound?
  51.     if not prototype then return end
  52.  
  53.     local chosen = PickFrom(prototype)
  54.     if not chosen then return end
  55.  
  56.     local oneshot = Randomizer.MakeOneshot(chosen)
  57.     if from then
  58.         if typeof(from) == "Instance" then
  59.             oneshot.Parent = from
  60.         else
  61.             local part = MakeTempPart(from)
  62.             oneshot.Ended:Connect(function() part:Destroy() end)
  63.             oneshot.Parent = part
  64.         end
  65.     end
  66.     return oneshot
  67. end
  68.  
  69. local module = {
  70.     ["PlayWithVisuals"] = function(prototype: Instance, from: Positionable?)
  71.         local sound = CreateOneShot(prototype, from)
  72.         if not sound then return end
  73.        
  74.         local sphere: SphereHandleAdornment = Instance.new("SphereHandleAdornment", sound)
  75.         sphere.Visible = true
  76.         sphere.Adornee = sound.Parent
  77.         sphere.Radius = sound.RollOffMinDistance
  78.        
  79.         sound:Play()
  80.     end,
  81.     ["Play"] = function(prototype: Instance, from: Positionable?)
  82.         local sound = CreateOneShot(prototype, from)
  83.         if not sound then return end
  84.         sound:Play()
  85.     end
  86. }
  87.  
  88. return module
  89.  
Advertisement
Add Comment
Please, Sign In to add comment