Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Randomizer = require(script.SoundRandomizer)
- local rng = Random.new()
- local function GatherSounds(inst: Instance)
- local list = {}
- if inst:IsA("Sound") then
- table.insert(list, inst)
- end
- for _, i in pairs(inst:GetDescendants()) do
- if i:IsA("Sound") then
- table.insert(list, i)
- end
- end
- return list
- end
- local function PickFrom(inst: Instance) : Sound?
- local sounds = GatherSounds(inst)
- if #sounds == 0 then return nil end
- return sounds[rng:NextInteger(1, #sounds)]
- end
- type Positionable = Part | Attachment | Vector3 | Vector2
- type Vec = Vector2 | Vector3
- local function ToVector3(at: Vector2) : Vector3
- return workspace.Camera:ScreenPointToRay(at.X, at.Y, 50).Origin
- end
- local function MakeTempPart(at: Vec) : Part
- local part = Instance.new("Part")
- part.CanQuery = false
- part.CanTouch = false
- part.CanCollide = false
- part.Transparency = 1
- part.Size = Vector3.new(1, 1, 1)
- part.Anchored = true
- if typeof(at) == "Vector3" then
- part.Position = at
- else
- part.Position = ToVector3(at)
- end
- part.Parent = workspace
- return part
- end
- local function CreateOneShot(prototype: Instance, from: Positionable?) : Sound?
- if not prototype then return end
- local chosen = PickFrom(prototype)
- if not chosen then return end
- local oneshot = Randomizer.MakeOneshot(chosen)
- if from then
- if typeof(from) == "Instance" then
- oneshot.Parent = from
- else
- local part = MakeTempPart(from)
- oneshot.Ended:Connect(function() part:Destroy() end)
- oneshot.Parent = part
- end
- end
- return oneshot
- end
- local module = {
- ["PlayWithVisuals"] = function(prototype: Instance, from: Positionable?)
- local sound = CreateOneShot(prototype, from)
- if not sound then return end
- local sphere: SphereHandleAdornment = Instance.new("SphereHandleAdornment", sound)
- sphere.Visible = true
- sphere.Adornee = sound.Parent
- sphere.Radius = sound.RollOffMinDistance
- sound:Play()
- end,
- ["Play"] = function(prototype: Instance, from: Positionable?)
- local sound = CreateOneShot(prototype, from)
- if not sound then return end
- sound:Play()
- end
- }
- return module
Advertisement
Add Comment
Please, Sign In to add comment