[SOLVED] SurfaceGui textbutton doesn't print anything when pressed

The SurfaceGui I have in my game is in a startergui and the adornee is set to the right part in the workspace. I have tried everything I could from putting the localscript in StarterPlayerScripts, inside the Textbutton, inside StarterGui I even checked the code 5 times for any spelling mistakes or defining a nonexisting part in the game. I have no longer any idea what to do, because I have a different SurfaceGui in my game and it works without any fault. Please help me out here.

The code I use (its located inside StarterPlayerScripts

ocal cam = workspace.CurrentCamera
local mouse = game:GetService("Players").LocalPlayer:GetMouse()
local player = game:GetService("Players").LocalPlayer
local buttondeb = false
local UserInputService = game:GetService("UserInputService")

game.Workspace.ScriptPart1.SurfaceGui.MainFrame.TextButton.MouseButton1Click:Connect(function()
	print("TextButton Clicked") -- Debug
	game.Workspace.IntroScene.ms1:Play()
	game.StarterGui.BlackScreen.BlackFrame.Visible = true
	game.Workspace.IntroScene["Vintage 1960s"].Body.Sound:Stop()
	game.Workspace.IntroScene.Lightbulb.Bulb["Buzzing Fluorescent Light"]:Stop()
	game.StarterGui.LoadingScreenScript:Destroy()
	game.StarterGui.ChangeCameraScript:Destroy()
	game.StarterGui.MainMenuCameraScript:Destroy()
	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	UserInputService.MouseIconEnabled = false
	task.wait(0.3)
	game.Workspace.IntroScene["annunciator beep"]:Play()
	repeat
		wait()
		cam.CameraType = Enum.CameraType.Custom
	until
	cam.CameraType == Enum.CameraType.Custom

	task.wait(5)

	game.Workspace.MainFacility.CardDoor.Main.bang:Play()
	task.wait(0.9)
	game.TextChatService.AmbienceScript.Parent = game.ServerScriptService
	game.TextChatService.FirstPersonBob.Parent = game.StarterPlayer.StarterCharacterScripts
	game.TextChatService.FlashlightScript.Parent = game.StarterPlayer.StarterCharacterScripts
	game.StarterGui.BlackScreen.BlackFrame.Visible = false

	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	UserInputService.MouseIconEnabled = false
	script:Destroy()

end)

2 Likes

SurfaceGui is not in workspace, you have to point it to PlayerGui by doing player.PlayerGui.SurfaceGui or having the script inside of the SurfaceGui and doing script.Parent.

2 Likes

I changed it to player.playegui.SurfaceGui but it still doesn’t even print the “TextButton Clicked”

2 Likes

Try placing the script inside of the SurfaceGui and doing script.Parent. Another thing is that you have to point to PlayerGui and not StarterGui when button is clicked. This would all be easier by just placing the script inside the SurfaceGui and pointing to PlayerGui by doing playergui = script.Parent.Parent.
The reason why it might not work now is because script is probably running before button is loaded, so you can add a bit of a delay (by adding a task.wait(5)) before connecting the event to test that and add :WaitForChild to make sure the button is loaded like this:

local button = script.Parent:WaitForChild("MainFrame"):WaitForChild("TextButton")
--assuing that the script is within `SurfaceGui`
button.MouseButton1Click:Connect(function()
    --do stuff
end)
1 Like

No, still nothing. I added :WaitForChild but it still doesn’t print anything.

1 Like

Can you screenshot the current hierarchy of where the script is, and how the script currently looks? Also is there any errors in the output, and are you sure output is displaying all messages?

2 Likes

Here is the hierarchy:
image

And the script:

task.wait(5)
local cam = workspace.CurrentCamera
local mouse = game:GetService("Players").LocalPlayer:GetMouse()
local player = game:GetService("Players").LocalPlayer
local buttondeb = false
local button = script.Parent:WaitForChild("MainFrame"):WaitForChild("TextButton")
local UserInputService = game:GetService("UserInputService")
task.wait(5)

button.MouseButton1Click:Connect(function()
	print("TextButton Clicked") -- Debug
	game.Workspace.IntroScene.ms1:Play()
	game.StarterGui.BlackScreen.BlackFrame.Visible = true
	game.Workspace.IntroScene["Vintage 1960s"].Body.Sound:Stop()
	game.Workspace.IntroScene.Lightbulb.Bulb["Buzzing Fluorescent Light"]:Stop()
	game.StarterGui.LoadingScreenScript:Destroy()
	game.StarterGui.ChangeCameraScript:Destroy()
	game.StarterGui.MainMenuCameraScript:Destroy()
	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	UserInputService.MouseIconEnabled = false
	task.wait(0.3)
	game.Workspace.IntroScene["annunciator beep"]:Play()
	repeat
		wait()
		cam.CameraType = Enum.CameraType.Custom
	until
	cam.CameraType == Enum.CameraType.Custom

	task.wait(5)

	game.Workspace.MainFacility.CardDoor.Main.bang:Play()
	task.wait(0.9)
	game.TextChatService.AmbienceScript.Parent = game.ServerScriptService
	game.TextChatService.FirstPersonBob.Parent = game.StarterPlayer.StarterCharacterScripts
	game.TextChatService.FlashlightScript.Parent = game.StarterPlayer.StarterCharacterScripts
	game.StarterGui.BlackScreen.BlackFrame.Visible = false

	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
	UserInputService.MouseIconEnabled = false
	script:Destroy()

end)

Are you sure you are waiting long enough for delay to end (you can also remove the delay now as you have the :WaitForChild)? It should work unless theres spelling mistakes (but output would error if thats the case). Also again everywhere you have game.StarterGui you have to replace with player.PlayerGui or point to playergui by doing local playergui = script.Parent.Parent.

1 Like

Yes because this isn’t where the game starts and the loading screen takes about 10-20 seconds, the output doesn’t output any errors that are leading to this script or SurfaceGui and I don’t think its the game.StarterGui that is wrong because print() is on the beginning of the function.

I honestly dont know what could be wrong as I have tested your code and it works fine, you could try placing a new part and making it SurfaceGui’s adornee, it could be that some other script is messing with it. game.StarterGui isnt the problem for this specific case, but it will become a problem later as you are not pointing to players gui, you are pointing to gui thats replicated to every player (so making changes to it wont do anything), thats why you should point it to the players gui.

2 Likes

I figured it out somehow, I did

player.playegui:WaitForChild("SurfaceGui"):WaitForChild("MainFrame"):WaitForChild("TextButton")

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