Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Event not returning the player?

Asked by
Troidit 253 Moderation Voter
8 years ago
Edited 8 years ago

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

Log in to vote
2
Answered by 8 years ago

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

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? Troidit 253 — 8y
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. Monsieur_Robert 338 — 8y
1
I think I see where you're coming at. Thanks for the help! Troidit 253 — 8y
Ad
Log in to vote
-1
Answered by 8 years ago

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.

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

Answer this question