Grabbing Player User Server Sided

To the point, I am trying to grab the player username after they click on a TextButton.

This is how I am doing it:

script.Parent.MouseButton1Click:Connect(function(player)

end)

Now, let’s say if I wanted to print the player user using print(player.Name), But unfortunately, I am having an error saying it is trying to "attempt to index with nil with ‘Name’ ". Did roblox update their Lua or something? I was able to grab players’ users just fine back then using this but now I can’t…

You were likely using a ClickDetector, GuiButton.MouseButton1Click never returned a player.

In this case, you should detect when the player clicks the button from a LocalScript and fire a RemoteEvent to the server.

1 Like

So like, fire the remote event with the player username and then use a ServerSided script to do the rest?

You don’t get the player from MouseButton1Click on a text button, you might be getting confused with a Click Detector’s MouseClick method. I’m assuming this text label is in starter gui/player’s player gui, if so, you can just chain a whole bunch of .Parent's, or alternatively (this method doesn’t need the button to be in starter/player gui) use a remote event.

You don’t have to specifically fire their username in the parameters as Roblox automatically sends the player who fired the remote as the first argument to the server (However, you can if you want)

Yes

1 Like

You don’t need to pass the player’s username, the player who fired the remote event is the default 1st parameter in OnServerEvent
Ex.

--localscript
remoteEvent:FireServer()

--server script
remoteEvent.OnServerEvent:Connect(function(player)
   print(player.Name)
end)
1 Like

Alrighty, thanks for the information. I thought I could grab the player user using MouseButton1Click.

In a local script, if you want to grab the player, just do

local player = game.Players.LocalPlayer

After the event fire, in the local script line where it fires the remote event, I am getting an error saying “Argument 1 missing or nil”
The line is RemoteEvent:FireClient()

If you’re firing from a server to a client, you need to specify the player

game.ReplicatedStorage.RemoteEvent:FireClient(ScriptedDevPerson, info)
-- info isn't required but you can put a value in here.

THe whole point of the script is grabbing the player username and using it to delete a specific thing.

1 Like

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