How to see what player clicked a gui button?

Hello everyone! I am trying to run a FireClient from a script under a TextButton. Script:

function leftClick()
	print("click")
	game.ReplicatedStorage.RemoteEvent2:FireClient(Player)
end

script.Parent.MouseButton1Click:Connect(leftClick)

However, it does not seem to work. Error, “FireClient: player argument must be a Player object”. I want to put in a player, but I want that to be the player who clicked the button. How do I do that?

Gui code can only be in client. Do remote events to the server

But doesn’t that trigger all clients?

You would use :FireAllClient instead.

could you do something like this for it to work?

No. Client side is only on the client. Do it in a local script patented under your gui and if the server needs to do anything, do RemoteEvent2:FireServer(info)

What it mean you haven’t define “Player”. You can either define it by local player or the player service.

It says: Fireserver can only be called from the client…

I tried localplayer but it did not work.

Also gui is client side and cannot be used in server scripts.

Make a local script and use the code there.

I suggest using the information as a guide about remote events:
https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

No error codes this time. Now i need to recieve it. That does not work yet.

local localPlayer = game.Players.LocalPlayer
local ExitButton = game.Players.LocalPlayer.PlayerGui.ScreenGui.TextButton

game.ReplicatedStorage.RemoteEvent2.OnServerEvent:Connect(function(Player)
	print("recieved")
	
	workspace.CurrentCamera.CameraSubject = localPlayer.Character.Humanoid

	ExitButton.Visible = false
		
end)

(This is a localscript inside of the StarterPlayerScripts by the way)

Ah, LocalPlayer can be only be defined from client, not server script.

I did that. Got a little confused :sweat_smile:

Hold on, if this is a local script (client-side) then you cannot to OnServerEvent, however you can do it from the server-side script.

So I need to change it to a normal script or something? I have another script with somewhat of the same goal in StarterPlayerScripts, and that one does work. However that one uses a OnClientEvent.

…I’m just going to write the code
Localscript

local localPlayer = game.Players.LocalPlayer
local ExitButton = game.Players.LocalPlayer.PlayerGui.ScreenGui.TextButton


ExitButton.MouseButton1Click:Connect(function(Player)
	print("recieved")
	
	workspace.CurrentCamera.CameraSubject = localPlayer.Character.Humanoid

	ExitButton.Visible = false
		
end)

The gui is in the player’s PlayerGui, right? Just get the parent of the Playergui’s parent and you get the player.

OnClientEvent is only used when a script (server-side) fired a remote event to the client-side. You would use this for local script (client-side) when getting the events that is fired from the server-side.

OnServerEvent is only used when a local script (client-side) fired a remote event to the server-side. You would use this for server-side (script) when getting the events that is fired from the local script (client-side).

1 Like