SurfaceGUI Button

Hi all,
I am trying to make a wall GUI (using SurfaceGUIs) to control the lights in my hotel room, and I think I know how. The only problem is I cant seem to get it to know its clicked in the first place. This is my script:

local text = script.Parent.TextLabel
local Button = script.Parent
Box = script.Parent.Parent.Box

function onClick()
	

		text.Text = "On"
		

		text.Text = "Off"
	
	end


Button.MouseButton1Click:connect(onClick)

I currently am only trying to get the text to change, so I know that it is working.

Any help would be appreciated, im sure its a really simple fix however i am unsure. Any help would be greatly appreciated!

You might need to apply some variable to your script first.

local text = script.Parent.TextLabel
local Button = script.Parent
Box = script.Parent.Parent.Box
local Enabled = false

function onClick()
	if not Enabled then 
		Enabled = true
		text.Text = "On"
	else
		Enabled = false
		text.Text = "Off"
	end
end

Button.MouseButton1Click:connect(onClick)

I added a variable name Enabled and it will change its value once someone click on it to true then it will change back to false after someone click on it again and then true and so on.
(Sorry for my bad explanation about this)

It doesnt seem to work? Ive added a print function so I can see if it works at all, and it does not.

The code worked for me.Screen Shot 2563-05-16 at 17.01.06

Forgot to mention that I sort out the stuff in the gui and script too
heres the new script

local text = script.Parent.TextLabel
local Button = script.Parent.TextButton
local Enabled = false
text.Text = "Off"

function onClick()
	if not Enabled then 
		Enabled = true
		text.Text = "On"
	else
		Enabled = false
		text.Text = "Off"
	end
end

Button.MouseButton1Click:connect(onClick)

Oh mines a local script! Does it need to be normal?

Well it is suppose to be script
Local also works fine but any changes will only appear on your screen.

so I suggested you to not use local if you want all players to see what you see.

Hmmmm. It still doesnt work?
image

local text = script.Parent.TextLabel
local Button = script.Parent
local Enabled = false
text.Text = "Off"

function onClick()
	if not Enabled then 
		Enabled = true
		text.Text = "On"
	else
		Enabled = false
		text.Text = "Off"
	end
end

Button.MouseButton1Click:connect(onClick)

Thats fab! Seems to be working now! Thank you :slight_smile:

1 Like