How would you get the username of who called a remote?

I’m using a TextButton to call a remote. I’m wondering how i would get the username of who clicked the button sent with the remote as a value to be used in a global script.
I’m new to using remotes because I’ve never really had to use them before.

Here’s the script i’m currently using to call it:

script.parent.MouseButton1Click:Connect(function()
	local rs = game:GetService("ReplicatedStorage")
	local event = rs:FindFirstChild("Reset")
	event:FireServer()
end)

Thank you!

When you connect the server event to a function, the first argument should always be the player. You can use player.Name.

function blah(player)
    local playerName = player.Name
end
event.OnServerEvent:Connect(blah)
5 Likes

Thank you! I’ll try a few things out and see what happens.

When you connect to the server event does it have to be the function name or can it be something like player?

Whatever follows :Connect should (and I think has to be?) a function. If you didn’t want to make one like the above, you could just do

event.OnServerEvent:Connect(function(player)    stuff here    end)

Oh I see. I misunderstood and completely didn’t see that it was calling the function. Sorry about that lol

Alright so that totally worked like i wanted it to! Awesome. If you don’t mind me asking, is there a simpler substitute for using something like workspace[""..plr..""].Torso when trying to use a string or object value?

workspace[player.Name] should work, and if not, workspace:FindFirstChild(player.Name)

1 Like

Thank you so much! It’s really annoying to type so many quotation marks and periods.

This is a classic XY problem.

You wanted the player who called the remote’s torso. You thought you could get that if you had their name to search for their character. You didn’t know how to do that. Instead of asking how to get the torso/character of the player who called the remote, you asked how to get the name. As a result, you failed to realize the question you asked is a bad one.

Getting the player’s character by their name opens up bugs where the player coincidentally (or with malicious intent) changes their name to the name of an object in your Workspace. Then all of a sudden, your script breaks.

If you want the Torso of the player, all you need to do is:

player.Character.Torso

3 Likes

I see. That does help my script be a bit less cluttered, but I don’t have any problems without it yet. I do see how this XY problem relates to my question, but i was not specifically trying to get the torso. That was simply an example. I’ll try to be more exact and proper next time. Also thank you for the advice on how someone can break the script, it should be helpful in the future!

Yeah, that’s about right, what lArekan said. The client who calls a remote will always be passed as the first argument. You also must include a variable for the player at the beginning of your connected functions to remotes. Skipping over that argument means whatever first argument you list will get overwritten by the player and anything else gets passed as excess (which you can’t index without ... at the end of your arguments list, or enough variables to cover for).

When you include a variable for the player:

-- Code related to remote
event.OnServerEvent:Connect(function (player, importantArg, ...)
	print(player, importantArg, unpack(...))
end)

-- Code on the client
event:FireServer("Important", "lol", "hi") --> Important lol hi

-- player = player
-- importantArg = Important
-- varArgs = lol, hi

When you don’t include a variable for the player:

-- Code related to remote
event.OnServerEvent:Connect(function (importantArg, ...)
	print(importantArg, unpack(...))
end)

-- Code on the client
event:FireServer("Important", "lol", "hi") --> player Important lol hi

-- importantArg = player
-- varArgs = Important, lol, hi

The same case goes for RemoteFunctions, except with their respective invoke method - player is always the first argument on the server.

Make sure not to make the same mistake on the client though, the player is never passed back to the client. You can forego the initial player argument requirement.

1 Like