Files
Hunter/scripts/ButtonAudio.client.luau
T
2026-11-29 19:48:57 +05:00

55 lines
1.7 KiB
Luau

--!strict
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Sounds = require(ReplicatedStorage:WaitForChild("SniperEffects") :: ModuleScript)
local playerGui = Players.LocalPlayer:WaitForChild("PlayerGui")
local function prepareCue(name: string, startSeconds: number): () -> ()
local def = Sounds.SOUNDS[name]
local sound = Instance.new("Sound")
sound.Name = "Sniper" .. name
sound.SoundId = def.id
sound.PlaybackSpeed = def.speed
sound.Parent = game:GetService("SoundService")
script.Destroying:Once(function() sound:Destroy() end)
return function()
if not sound.IsLoaded then return end
local volume = def.volume * math.max(Sounds.setting("VolMaster", 1), 0)
* math.max(Sounds.setting("Vol" .. def.group, 1), 0)
if volume <= 0 then return end
sound:Stop()
sound.Volume = volume
sound.TimePosition = startSeconds
sound:Play()
end
end
-- Start each recording just before its measured onset.
local playHover = prepareCue("ButtonHover", .38)
local playPress = prepareCue("ButtonPress", .035)
local bound: {[GuiButton]: boolean} = setmetatable({}, {__mode = "k"})
local function bindButton(instance: Instance)
if not instance:IsA("GuiButton") or bound[instance] then return end
local button = instance
bound[button] = true
button.MouseEnter:Connect(function()
if button.Active and button.Interactable and button:IsDescendantOf(playerGui) then
playHover()
end
end)
button.Activated:Connect(function()
if button:IsDescendantOf(playerGui) then
playPress()
end
end)
end
playerGui.DescendantAdded:Connect(bindButton)
for _, instance in playerGui:GetDescendants() do
bindButton(instance)
end