Files
2026-11-16 23:50:40 +03:00

58 lines
2.2 KiB
Luau

-- UISounds (LocalScript, StarterPlayerScripts): sounds for every button of the interface.
-- hover (mouse only) -> SoundService.UISounds["Cream keyboard"]
-- click / tap -> SoundService.UISounds["keyboard click"]
-- Works on all buttons in PlayerGui, also the ones the UI scripts create later.
-- A button with the attribute NoSound = true stays silent (invisible blockers of the tutorial, etc.).
local Players = game:GetService("Players")
local SoundService = game:GetService("SoundService")
local UIS = game:GetService("UserInputService")
local player = Players.LocalPlayer
local pg = player:WaitForChild("PlayerGui")
local folder = SoundService:WaitForChild("UISounds", 10)
local hoverSound = folder and folder:FindFirstChild("Cream keyboard")
local clickSound = folder and folder:FindFirstChild("keyboard click")
if not hoverSound and not clickSound then return end
local HOVER_GAP = 0.06 -- seconds: moving fast across a row of buttons must not machine-gun the sound
local lastHover, lastClick = 0, 0
local lastInputTouch = false
UIS.LastInputTypeChanged:Connect(function(t) lastInputTouch = t == Enum.UserInputType.Touch end)
local function play(sound)
if sound then SoundService:PlayLocalSound(sound) end
end
local function silent(b)
return b:GetAttribute("NoSound") == true
end
local done = setmetatable({}, { __mode = "k" })
local function hookButton(b)
if done[b] or not b:IsA("GuiButton") then return end
done[b] = true
b.MouseEnter:Connect(function()
-- on touch screens MouseEnter fires on the tap itself: only the click sound there
if lastInputTouch or not UIS.MouseEnabled or silent(b) or not b.Active then return end
local now = os.clock()
if now - lastHover < HOVER_GAP then return end
lastHover = now
play(hoverSound)
end)
b.Activated:Connect(function()
if silent(b) then return end
local now = os.clock()
if now - lastClick < 0.03 then return end -- one press = one sound
lastClick = now
play(clickSound)
end)
end
local function hookGui(gui)
if not gui:IsA("LayerCollector") then return end
for _, d in gui:GetDescendants() do hookButton(d) end
gui.DescendantAdded:Connect(hookButton)
end
for _, g in pg:GetChildren() do hookGui(g) end
pg.ChildAdded:Connect(hookGui)