Why is my button not sending to the output?

So, I am trying to make a test button, before I start doing what I am trying to do, once you click it it is supposed to send to the output “Button Pressed!” But it isnt.

My Script is:

local MainGui = game.StarterGui.MainGui

local Main = game.StarterGui.MainGui.Main

local Off = game.MainGui.Main.OffButton

Off.MouseButton1Click:Connect(function()

print (“Button Pressed!”)

end)

The Directory, looks like:
Anyone

Any suggestions?

Because you’re waiting for events in StarterGui which is only a placeholder for GUIs to be replicated to clients when they join. The GUIs player’s see are stored in their PlayerGui folder which is in their Player instance. Otherwise your code seems to work fine.

The Developer Hub website is linked here if you need to know more.

local MainGui = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui"):WaitForChild("MainGui")

This would be an example of what to replace with your current MainGui code.

local MainGui = game.Players.Localplayer.PlayerGui.MainGui

local Main = game.Players.Localplayer.PlayerGui.MainGui.Main

local Off = game.Players.Localplayer.PlayerGui.MainGui.Main.OffButton

Off.MouseButton1Click:Connect(function()

print (“Button Pressed!”)

end)

That won’t work. You can’t use StarterGui.

Oops. My bad (30 chartssssssssssss)

So, then It would have to be like

local MainGui = game.Players.Localplayer.PlayerGui.MainGui

local Main = game.Players.Localplayer.PlayerGui.MainGui.Main

local Off = game.Players.Localplayer.MainGui.Main.OffButton

Off.MouseButton1Click:Connect(function()

print (“Button Pressed!”)

end)

Yes, just make sure to include :WaitForChild("Child Name Here") since when it comes to stuff on the client it will take sometime for everything to load in perfectly and may lead to issues if your scripts load before your GUIs replicate.

You couldve just did script.Parent.MouseButton1Click and it would work.

Yes, but I like to make code that doesn’t depend on the location of the script. Since it’s inefficient to have a separate script for each button. Also, I just like to keep organized so trying to keep scripts in StarterPlayerScripts is what I do.

You dont need to make a script for each button… You can just do something like

local offButton = script.Parent
local mainFrame = offButton.Parent
local mainGUI = mainFrame.Parent

and then you can just refer others.