How to get player from button clicked

So I have a server script which spawns in a Surface GUI which lists all the servers available and you can pick a specific server to join but I have a problem trying to get the player from the server script since you cannot get the player from a text button click, does anyone know a way or do I have to use local scripts.

You don’t. You’re supposed to handle everything related to the UI on the client.

local plr = game:GetService(“Players”).LocalPlayer

local plr = game:GetService(“Players”).LocalPlayer

Wouldn’t work in a server script.

You could either move the script to a local script and correctly reference every necessary UI instance of the SurfaceGui and place the script inside StarterPlayerScripts/StarterCharacterScripts folder or you could keep the server script and make use of a RemoteEvent, you should probably go for the former of those two options.

ow yeah forgot u most use remote event

local R_E = game.ReplicatedStorage.RemoteEvent

WhateverButton.MouseButton1Click:Connect(function()
      R_E:FireServer() 
end)

On the Server:

game.replicatedSotrage.RemoteEvent,OnServerEvent:Connect(function(player) -- this will give the player who fired the remote event, aka the player who clicked the TextButton
      --you can use another remote with FireClient to send the player back to the client (pretty sure there is a better way of doing this)
end)

Also everything GUI related should be handled on the client

1 Like

use ClickDetectors?