What is the best way to get a player?

  1. I want a button on a surface gui. The problem I need to get the player. What is the best way to get a player when they click on a button on a surface gui. (A script not a local script)

  2. I asked some people and gave no response.

2 Likes

You should treat a surface GUI like any other client-sided UI; Use a localscript to listen for a click.

1 Like

Mhm. I am trying to make a system more on the server side not really client side. Remeber it is a surface gui on a part.

1 Like

‘’’
script.Parent.MouseButton1Down:Connect(function(plr)
Player = plr
End)
‘’’

Sorry this was wrote on mobile

1 Like

There is a plr parameter in mousebuttondown. :open_mouth: Let me check.

1 Like

I would guess so, never created a server side gui.

1 Like

Anything GUI related should really be dealt with client-side, as it is redundant using the server to do such tasks. If you really need the server to interact for this scenario, you can just use a remote event/ function to do so. Plus, it sends the player object as you initially wanted!

local GUI = workspace.ur.gui
local button = GUI:WaitForChild("your_Button")
local Player = game:GetService("Players").LocalPlayer

local Remote = ur.Remote

button.MouseButton1Click:Connect(function()
     Remote:FireServer()
end)


-- Server
local remote = ur.remote

remote.OnServerEvent:Connect(function(Player)
    print("Voila! I got the player object! The player's name is  " .. Player.Name)
end)
6 Likes

Remeber tho. This is a textbutton on a surface gui.

1 Like

I know one method but just want to know if there is a better method.

1 Like

Your trying to get the Player from the Server-side, correct? If so then,

game.Players.PlayerAdded:Connect(function(player)

--[[ do stuff here]]

end)
2 Likes

No like there is a button on a surface gui. The person clicks on the button. What is the best way to get the player that clicked on the button?

1 Like

As what everybody else has been saying, the best option is for it to be Client-Sided.

( Local Script )

2 Likes

It doesn’t matter that it’s on a surface GUI, you should still use remotes and a local script. What other method are you using?

1 Like

So put a local script in starter gui and if the player clicks on the button it just fire a remove event?

1 Like

That was the method I was gonna use but just wanted to know if there is a better way.

1 Like

I assure you this is the best way. I believe there is no way to detect the player who clicked the button on the server anyways.

1 Like

Just use the method that AbiZinho just shown you:

2 Likes

You can put a local script in the Surface GUI and use the same code and fire the server using AbiZinho’s method. Treat it like it was in the player’s StarterGui.

1 Like

Yea I will need to make it client sided. Thx to everyone who helped me.

1 Like

Put the SurfaceGui in StarterGui, then set its adornee to the part you want it to be on. From there you can listen for inputs from a localscript.

2 Likes