TextButton doesn't work

  1. What do you want to achieve?
    I would like the script to execute after clicking a button in the gui

  2. What is the issue?
    The button does not respond when clicked

VIDEO

CODE

local StructureFrame = game.StarterGui.NodeBuilding:WaitForChild("StructureFrame")

for _, structureButton in pairs(StructureFrame:GetChildren()) do
	if structureButton:IsA("TextButton") then
		print(structureButton)
		structureButton.MouseButton1Up:Connect(function()
			print("click")
		end)
	end
end


local Button = game.StarterGui.NodeBuilding.StructureFrame:WaitForChild("TextButton")

Button.MouseButton1Up:Connect(function()
	print("click")
end)
  1. What solutions have you tried so far?
    I try roblox developer portal but nothing there works for me

The Gui inside of StarterGui is not the same Gui that the player sees. You’ll need to get the Gui that’s inside of the player’s PlayerGui:

local Players = game:GetService("Players")

local NodeBuilding = Players.LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("NodeBuilding")

local StructureFrame = NodeBuilding:WaitForChild("StructureFrame")

for _, structureButton in pairs(StructureFrame:GetChildren()) do
	if structureButton:IsA("TextButton") then
		print(structureButton)
		structureButton.MouseButton1Up:Connect(function()
			print("click")
		end)
	end
end


local Button = StructureFrame:WaitForChild("TextButton")

Button.MouseButton1Up:Connect(function()
	print("click")
end)
2 Likes

Ohh… okey I made a small mistake and didn’t notice that, thanks for your help

1 Like

By the way, there’s an alternative method you can do to achieve the same result as the current script:

  • Instead of the LocalScript’s parent being StarterGui, have the parent be the NodeBuilding GUI itself
  • Afterwards, replace the script’s code with this:
local NodeBuilding = script.Parent -- Make sure that the LocalScript's parent is the NodeBuilding GUI!
local StructureFrame = NodeBuilding:WaitForChild("StructureFrame")

local function onMouseButton1Up()
	print("click")
end

local function onChildAdded(instance)
	if instance:IsA("TextButton") then
		print(instance)

		instance.MouseButton1Up:Connect(onMouseButton1Up)
	end
end

StructureFrame.ChildAdded:Connect(onChildAdded) -- Some TextButtons might not have finished loading
-- in time for players whose devices have limited performance. ChildAdded will detect when they're done loading

for _, instance in StructureFrame:GetChildren() do onChildAdded(instance) end -- In-order to get the TextButtons
-- that did finish loading in time

I usually store a GUI’s scripts as children of the GUI itself, since it makes it easier to reference any of the GUI’s contents :slight_smile::+1:

1 Like

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