MouseButton1Click doesnt work (no overlapping buttons, frames or anything else)

  1. What do you want to achieve? I want to create a main menu (with a surface gui)

  2. What is the issue? The problem is, that the MouseButton1Click function wont detect any clicks

  3. What solutions have you tried so far? I experimented with different code structures but nothing I tried seemed to work.

Explorer:
image

Script:

local players = game:GetService("Players")
local rps = game:GetService("ReplicatedStorage")
local rs = game:GetService("RunService")
local sg = game:GetService("StarterGui")
local uis = game:GetService("UserInputService")

local player = players.LocalPlayer
local playerGui = player.PlayerGui
local menuGuis = sg.Menu

local mainMenu = menuGuis.MainMenu:Clone()

mainMenu.Parent = playerGui

local RemotesFolder =  rps:WaitForChild("Remotes")

local Camera = workspace.CurrentCamera
local menuFolder = workspace:WaitForChild("Menu")
local cameraParts = menuFolder:WaitForChild("CameraParts")
Camera.CameraType = Enum.CameraType.Scriptable

local playButtonPressed = Instance.new("BoolValue")
playButtonPressed.Name = "PlayButtonPressed"
playButtonPressed.Value = false
playButtonPressed.Parent = playerGui:WaitForChild("Menu")

local GoTo = Instance.new("StringValue")
GoTo.Value = "MainMenu"
GoTo.Name = "GoTo"
GoTo.Parent = Camera

Camera.CFrame = cameraParts[GoTo.Value].CFrame

local NewScreenGui = Instance.new("ScreenGui", playerGui)
local TextButton = Instance.new("TextButton")

TextButton.BackgroundTransparency = 1
TextButton.Size = UDim2.new(0, 0, 0, 0)
TextButton.Text = " "
TextButton.Parent = NewScreenGui
TextButton.Modal = true

local playButton = sg.Menu.MainMenu.PlayButton

rs.RenderStepped:Connect(function()
	if playButtonPressed.Value == false then
		local GoToPosition = cameraParts[GoTo.Value].CFrame

		GoToPosition += Vector3.new(math.cos(tick() * 2) * 0.1, math.sin(tick() * 2) * 0.1, math.cos(tick() * 2) * 0.1)
		Camera.CFrame = Camera.CFrame:Lerp(GoToPosition, 1)
	else
		
	end
end)

--// Player Model \\--

local playerModel = players:CreateHumanoidModelFromUserId(player.UserId)
playerModel.Parent = menuFolder.PlayerPart
playerModel:ScaleTo(1.5)
playerModel:PivotTo(menuFolder.PlayerPart.CFrame)
playerModel.Parent = menuFolder.PlayerPart

playButton.MouseButton1Click:Connect(function()
	print("Mouse has been clicked")
	playButtonPressed.Value = true
	Camera.CameraType = Enum.CameraType.Custom
	uis.MouseBehavior = Enum.MouseBehavior.LockCenter

	TextButton:Destroy()
	menuFolder:Destroy()
	menuGuis:Destroy()
end)

--// Buttons \\--

--[[
local function regButton(Button: ImageButton)
	Button.MouseButton1Click:Connect(function()
		GoTo.Value = Button.Name
	end)
end
]]--

-- This part is out-commented because i dont need it rn

Thank you for your time! I appreciate and kind of help.

So it doesn’t print the “Mouse has been clicked” thing when you click the button?

According to what I see, it is because you are looking for the button in statergui instead of searching for it in playergui

1 Like

The button path isn’t correct, it comes from starterGui instead of the playerGui

Like @Sailexv and @PapeKDev have said, you are trying to get the events from the button in StarterGui. You need to get it from the player’s PlayerGui, which is a direct child of a player. Change the playButton variable to be in the player’s gui.

local playButton = playerGui.Menu.MainMenu.PlayButton

Yeah you guys are correct. I didnt realize it. Thanks for the help! It now works.

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