For loop connections not working?

Hello fellow developers,

I am creating a script in StarterGui that automatically adds sound effects when you hover or click on a button. The hierarchy is ordered like this:

image

I used :GetDescendants() and then used a for loop with ipairs(). I did this because of this topic, which talks about using connections with for loops using said method.
Script:

local starterGui = game:GetService("StarterGui")

local root = script.Parent
local click = root:WaitForChild("Click")
local hover = root:WaitForChild("Hover")

local objects = starterGui:GetDescendants()

print("Handler handling")

for _, object in ipairs(objects) do
	if object:IsA("TextButton") or object:IsA("ImageButton") then
		print("Found button")
		object.MouseEnter:Connect(function()
			hover:Play()
			print("Played hover")
		end)
		object.MouseButton1Click:Connect(function()
			click:Play()
			print("Played click")
		end)
	end
end

“Handler handling” and “Found button” both print perfectly fine, but the sounds don’t play and the “Played hover”/“Played click” don’t print. Am I doing this incorrectly? What is the issue here?
My apologies if this is dumb, surprisingly, I have never actually used a for loop before in my programming. I guess I found it too complicated when starting out and never remembered to try again later.

Thanks in advance!
Fizzitix

I think the error lies with StarterGui.

The StarterGui service doesn’t hold the buttons that are active on your screen while playtesting, they are just the instances that get copied over to every player when they join the game.

If you want to access the buttons on your screen, you need to access the player’s PlayerGui to do so, which you can see in the instance hierarchy while playtesting. Your player under Players will have a folder under it called PlayerGui I think, and you’ll be able to see your GUI under there.

Try redefining objects so it points to the descendants of the local player’s PlayerGui, something like this.

local Players = game:GetService("Players")

local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")

-- sounds

local objects = playerGui:GetDescendants()

-- loop through objects

If the resulting for loop doesn’t catch every button somehow, you can listen to the playerGui.DescendantAdded event and run your for loop body there.

1 Like

For future readers, I had to use the DescendantAdded method for whatever reason.

Script:

local players = game:GetService("Players")

local player = players.LocalPlayer
local playerGui = player.PlayerGui

local root = script.Parent
local click = root:WaitForChild("Click")
local hover = root:WaitForChild("Hover")

local objects = playerGui:GetDescendants()

print("Handler handling")

playerGui.DescendantAdded:Connect(function(object)
	if object:IsA("TextButton") or object:IsA("ImageButton") then
		print("Found button")
		object.MouseEnter:Connect(function()
			hover:Play()
			print("Played hover")
		end)
		object.MouseButton1Click:Connect(function()
			click:Play()
			print("Played click")
		end)
	end
end)

Anyways, thanks for the fix! This will speed up development a lot.

Sincerely,
Fizzitix

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.