How do I click a GUI with a script?

How do I send a mouse click on a Gui button with a script?

3 Likes

What?

Can you explain what you want more

2 Likes

Do you mean textbutton?

script.Parent.MouseButton1Click:Connect(function()
                 print("Clicked")
    end)

(has to be in a textbutton and has to be a localscript)

5 Likes

The only way to do such is through a Remote event because GUI’s on StarterGUI only run plainly with no Remote Events through a LOCAL Script which is ran on the client. There isn’t a lot of info you’ve given us but I’m guessing you want a GUI to open on the Server rather than just the client.

1 Like

You mean like MouseButton1Click? You can use it to do a function when an image/text button is clicked…if that’s what you mean.

do you mean that you want the script to fire Mouse click without the player actually clicking the TextButton?

4 Likes

Yes exactly thank you :smiley:

Well when would you want it fired?

MouseHover you mean?

1 Like

then you can’t use a mousclick event, you have to use a local function like this

local function ScriptFakeClick()
--do stuff
end

whatyouwillusetofiretheevent:Connect(ScriptFakeClick)

just copy the same code inside the mouseclick function and paste it to where you want it

You probably have a TextButton with a MouseButton1Click or Activated event that does something, and you want the event to fire as if it had been clicked, without the player clicking it.

local button = game.PlayerGui.ScreenGui.TextButton

function onActivated()
	print("asdf")
end
button.Activated:Connect(onActivated)

There are 3 ways to do that.
One is to make a custom BindableEvent and make it also connect to the same function, and then Fire the BindableEvent to simulate a click.

-- Script 1
local button = game.PlayerGui.ScreenGui.TextButton
local event = Instance.new("BindableEvent")
event.Name = "ClickAsdf"
event.Parent = game.ReplicatedStorage

function onActivated()
	print("asdf")
end
button.Activated:Connect(onActivated)
event.Event:Connect(onActivated)

-- Script 2
-- Makes touching the part named Button do the same as clicking the GUI button
local button = workspace.Button
local event = game.ReplicatedStorage.ClickAsdf
button.Touched:Connect(function()
	event:Fire()
end)

The second way is to expose the function that the button calls, and let other things call it.
I will be doing this with a ModuleScript, but you can also do _G.onActivated = onActivated in Script 1 if you want to shoot yourself in the foot. Anyway,

-- Script 1
local button = game.PlayerGui.ScreenGui.TextButton
button.Activated:Connect(require(game.ReplicatedStorage.ClickAsdf))

-- Script 2
-- Makes touching the part named Button do the same as clicking the GUI button
local button = workspace.Button
button.Touched:Connect(require(game.ReplicatedStorage.ClickAsdf))

-- ModuleScript 3
-- This is game.ReplicatedStorage.ClickAsdf
return function()
	print("asdf")
end

The third way is to just copy-paste the function. Eww!

-- Script 1
local button = game.PlayerGui.ScreenGui.TextButton

function onActivated()
	print("asdf")
end
button.Activated:Connect(onActivated)

-- Script 2
-- Makes touching the part named Button do the same as clicking the GUI button
local button = workspace.Button

function onActivated()
	print("asdf")
end
button.Touched:Connect(onActivated)
5 Likes

Uhhhhh you’re not giving me much information to work with, but here is something to start you off

--|| Vars
local button = script.Parent

--|| Main
button.MouseButton1Click:Connect(function()
      -- When the player clicks the button, do something.
end)

You would need to put this in a local script inside of a text button.

could you please explain in detail?

guys he means. fire a button event without actually pressing it. like with a tool you can do tool.Activate then the tool will react on a activation just like if the player would press left click

1 Like

I’m not sure if it is the right thing but if you wanna fire a mouse click without clicking it is not possible. But you can always call a function that’d do the same.

local button = script.Parent

local function MyFunction()
 print("Clicked")
end

button.MouseButton1Click:Connect(MyFunction) --//Calling with a mouse click

MyFunction() --//Calling without a mouse click

I might be wrong, but I think you might be able to simulate a click by using a Virtual User

I’ve implemented this workaround myself. though it’s pretty hacky. Wish we had a built-in solution… (ex. GuiButton:Activate())

3 Likes

BindableEvent worked like a charm (I just had to be careful to specify context to be client) and you explained in a much simpler way than the official docs.

I got a work around. Make a “BindableEvent” put it in the button and call it “buttonClick” or something anything you like.

then fire that event whenever the button is clicked.

and connect your function to that event instead of to the button.

now if you want to run the function of that button without clicking it. You just do button.[“the bindable event you made”]:Fire()