What do you want to achieve?
I would like the script to execute after clicking a button in the gui
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)
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)
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