Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Event not returning the player?

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.

2 answers

✓ Accepted answer

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).

3 comments

0

I appreciate your answer as it sounds like it would work. But the SurfaceGui is located inside the Workspace because I need everyone to be able to see which player clicked the button. With Filtering Enabled active, it wouldn't show the change to anyone else but the client?

1

Oh, then just have a localscript where when the player clicks on the button, it fires up a remoteevent to the server telling the server that a player clicked on the button. Then you can handle it from there.

1

I think I see where you're coming at. Thanks for the help!

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.

1 comment

0

I still get the same result when I use your parameters instead. "attempt to index local 'plr' (a nil value)"

This discussion is preserved as it was. New answers, comments, and votes are closed.