Hiding a frame using a ClickDetector and a RemoteEvent

I have a part that when clicked with a ClickDetector, makes a frame visible in a GUI inside of PlayerGui.

when that part is in a visible state, clicking the part sends a RemoteEvent to a LocalScript in the GUI holding the frame, and then that LocalScript makes the frame non-visible.

The thing is that the RemoteEvent fires with the player, but does not have the player in the LocalScript.

Part (ServerScript):

Part.ClickDetector.MouseClick:Connect(function(click)
	if visible == true  then
		game.ReplicatedStorage.CloseFrameRE:FireClient(click)

Script in GUI holding frame (LocalScript):

game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function(click)
	click.PlayerGui.GUI.Frame.Visible = false
end)

How do I get the player so I can make the frame invisible for the player?

Localscripts just require Players.LocalPlayer

local Players = game:GetService("Players")

local player = Players.LocalPlayer

-- Your code to make the frame visible

Passing the player in FireClient just tells it what player should receive the event call, it doens’t pass it to the OnClientEvent since it’s easy to get the player with a simple property, keep in mind it does not work on the Server as LocalPlayer will return nil

You don’t need to get your own player as an argument, remember, you’re running that code on the client and you are already able to get your own player object via the LocalPlayer property of the Players service

However, I highly suggest doing this entirely from the Client in the first place, since there is no reason (from the given context) to even have the server involved in this action.

So just listen for the click detector from the client in the first place, e.g

-- Client
yourClickDetector.MouseClick:Connect(function()
      yourFrame.Visible = not yourFrame.Visible --This will toggle the visible property to the opposite of what it initially was
end)
1 Like