So, I have a script that loops through the StarterGui’s children, and then loops through all the ScreenGuis’s children, and if it found a TextButton instance inside the ScreenGuis, then the sound will play when the TextButton is clicked
Here’s the script but it doesn’t work
local StarterGui = script.Parent
local Sound = StarterGui:WaitForChild("HoverCursorOnAnswerSound")
for i, v in pairs(StarterGui:GetChildren()) do
if v:IsA("ScreenGui") then
for index, value in pairs(v:GetChildren()) do
if value:IsA("TextButton") then
value.MouseButton1Click:Connect(function()
Sound:Play()
end)
end
end
end
end
One reason might be that the Sound ID is no longer working, or maybe you set the Volume to 0.
Another reason might be that you used a Script instead of a LocalScript?
While I’m at it, allow me to shorten up your code a bit:
local StarterGui = script.Parent
local Sound = StarterGui:WaitForChild("HoverCursorOnAnswerSound")
for _,textButton in pairs(StarterGui:GetDescendants()) do
if textButton:IsA("TextButton") then
textButton.MouseButton1Click:Connect(function()
Sound:Play()
end
end
end
I’m uncertain whether newly created sounds work right away. I believe it can take up to 24 hours for the sound to become fully operational. (Again. Don’t believe me on this.)
Edit: Instead, try a different sound ID first and see if that one plays.
From my testing it was an issue with replication order where the script is cloned first before the GUI. Adding a task.wait() fixed it although I believe there are other better methods such as :WaitForChild() some devs also prefer cloning GUI from replicated storage to avoid wait for child.
local StarterGui = script.Parent
--local Sound = StarterGui:WaitForChild("HoverCursorOnAnswerSound")
print(StarterGui)
print("Before Wait")
print(StarterGui:GetChildren())
task.wait()
print("After Wait")
print(StarterGui:GetChildren())
for i, v in pairs(StarterGui:GetChildren()) do
if v:IsA("ScreenGui") then
for index, value in pairs(v:GetChildren()) do
if value:IsA("TextButton") then
print("Text button found", value)
value.MouseButton1Click:Connect(function()
print("Clicked")
--Sound:Play()
end)
end
end
end
end