How can I detect an input from a GUI element while the script is in workspace?

Hey there! I need a little help here.
So, I don’t know how I can detect if a player pressed a GUI element while the script is in the workspace. Basically, when I press a GUI button, it should just, let’s say, print something to the output, except that the script identifies who pressed it while it’s in workspace.

I don’t know if PlayerAdded helps, or GetPlayerFromCharacter, I just need help. If you need something from me, please tell me.

Thanks!

The first thing I could think of is using events maybe?

Place the LocalScript in a container where it can run, then path to the GUI in workspace.

He wants to use a script in workspace and have it work with a GUI inside the player

…but I want to know which one exactly. Right now, it’s just a normal script, and I want to know how I can detect an activated element from a player.

1 Like

As I said, it’s a Script, not a LocalScript, also, I can’t put it in the button because it won’t work that way.

Anything related to player input with GUIs should be done in a LocalScript then forwarded to the server via a RemoteEvent if necessary.

You could use a RemoteEvent and fire it via a LocalScript in the player’s GUI and then the script in workspace could receive the event.

How could I perhaps detect if the event was fired? Perhaps do I have to create separate scripts or…?

1 Like

Use a server Script to listen to the RemoteEvent.OnServerEvent event.

-- workspace script

local event = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent") --or whereveevereefvsds it is

event.OnServerEvent:Connect(function()
	
end)

&

-- local script

local event = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent") --or whereveevereefvsds it is

event:FireServer()

These should get you started

I’ll try this out. Thanks for now!
I’ll reply if it works and give you a solution considering that it worked.

1 Like

Note that placing scripts as a descendant of Workspace is not a good practice.

1 Like

Thank you so much, I can’t explain to you how grateful I am for you. This helped me so much!

By the way, I gave you the solution AND I do understand your scripts. Thanks though!

1 Like

That’s true, it should be stored somewhere else, but right now these scripts can’t be placed elsewhere. Thanks for reminding!

Why can’t these scripts be placed elsewhere? You could use CollectionService if you need an easy method of tagging instances.

Well, if I dig for some time they can, but I don’t feel like placing them somewhere else. I do get it though.

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