I'm trying to make a simple script right now that returns the name of the player who clicked a SurfaceGui button and I know I'm doing something wrong. Here's the code I have for it.
button = script.Parent button.MouseButton1Click:connect(function(plr) button.Parent.CurrentHostText.Text = "Current Host: " ..plr.Name end)
My output says "Attempt to index local 'plr' (a nil value)". Does MouseButton1Click not return the player who clicked it? If so, is there another way to accomplish this then?
EDIT
I forgot to mention that Filtering Enabled is enabled.
You are correct. MouseButton1Click does not pass in the player who clicked the button. The solution to this is that if you are using a LocalScript (which you should be, although it is not required), just simply use game.Players.LocalPlayer as your player. If running on a server script, you can get the Player that the gui is inside of. For example, your gui might look like game --> Players --> Player --> PlayerGui --> Gui. If your script is in game.Players.Player.Gui, then script.Parent.Parent will give you the player (this is just an example).
The first argument that you pass to the server will actually be the second parameter in the connected function, since the first parameter is reserved for something else (player maybe?).
button = script.Parent button.MouseButton1Click:connect(function(player,plr) button.Parent.CurrentHostText.Text = "Current Host: " ..plr.Name end)
Not tested, but that should get the result.