SurfaceGui TextButton not working sometimes

I have put a surfacegui into a part in workspace by setting the surfacegui’s adornee to the part, however the textbutton (.Activated) does not work all the time. I am unsure whether this is a scripting error or a bug since the button works sometimes.

LocalScript:

local TextButton = script.Parent:WaitForChild('TextButton')

TextButton.Activated:Connect(function()
	print('button pressed')
end)

image

Video of the button working:

3 Likes

Instead of using .Activated, I would use .MouseButton1Click.

local TextButton = script.Parent:WaitForChild('TextButton')

TextButton.MouseButton1Click:Connect(function()
	print('button pressed')
end)

I’m not sure why .Activated isn’t always working for you, but I prefer to use .MouseButton1Click anyway, so you can try this and see if it works.

2 Likes

This is what I tried first, and it didn’t work either.

1 Like

Is the surface gui the child of a part in the workspace?

No, the surfacegui is in startergui.

image

Why is it a SurfaceGui instead of a StarterGui?

The surfacegui is set to a part in workspace, as seen in the adornee. It is in startergui so that I can get the player from the textbutton being activated.

Instead of detecting when the button is clicked, you can try detecting when the part that the SurfaceGui is adorneed to is clicked.

local part = workspace.Part -- Change this to reference your part
local ClickDetector = part.ClickDetector

ClickDetector.MouseClick:Connect(function()
	print('button pressed')
end)

Make sure to place a click detector inside of the part that you want to be clicked.

I used this in the past but it would be unrealistic for what I am trying to do since the screen would be changing as well as the location of the button.

maybe try setting the active property of all other elements to false

It might be the ZIndex but probably not

I set it to a high number already

Try using this edit to your script with print statements, and let me know what gets printed in the output.

local TextButton = script.Parent -- Change this to your text button name/path
local AdorneePart = workspace.Part -- Change this to your part name/path

if AdorneePart then
	-- Check if the TextButton is adorning the specified part
	local adorn = AdorneePart:FindFirstChildOfClass("TextLabel")
	if adorn and adorn:IsA("TextLabel") and adorn == TextButton then
		print('AdorneePart found')
		TextButton.Activated:Connect(function()
			print('Button pressed')
		end)
	else
		warn("The TextButton is not adorning the specified part.")
	end
else
	warn("AdorneePart not found.")
end

Maybe try putting it in a serverScript. That is working for me and I’m not sure if localscripts in the workspace is what you would want to be doing.

Edit: nevermind, you have it in startergui. Im a bit dumb

I think I found the problem: Set the AlwaysOnTop property of the SurfaceGui to be true, then change the face side of the SurfaceGui to be what you want, and the problem is fixed (it worked for me, anyway). You can keep your original script, and it will work.

image

This seems to be working, thanks.

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