Getting player when player clicks on button

Greetings,

How can I do so it gets the player when the player clicks on a button? I have my code down below, but it does not work, please help! Btw, this is a normal script, not a local one.

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

end)

Thanks a lot!

1 Like

If this is on client, you can do game.Players.LocalPlayer to get the player.

Oh yea I forgot to mention this is a normal script

If it is in the PlayerGui, you can keep doing .parent until you get to the Player.

1 Like

Is it a click detector or a ui?

Yea I never thought of that tbh. Thanks a lot, it worked.

This is bad practice - you’re assuming an unchanging hierarchy of Player.PlayerGui.x.y.z.Script when the hierarchy could be changed to Player.PlayerGui.x.y.z.w.f.p at runtime. A better solution would be to use FindFirstAncestorWhichIsA() or FindFirstAncestorOfClass(). And, if you’re using a localscript an even better solution would be to use Players.LocalPlayer.

If it’s a GUI you should use the “Activated” property rather than mouse button 1 click. Activated allows your code to work cross platform.

1 Like

That’s just going to give you a mess in the code, you can just do game.X. (X being the destination) It’s easier and in case of a bug, you can just resolve it immediately. But if we repeat script.Parent.Parent.Parent.Parent(and so on) it’s going to create a mess in our code, and incase of a bug we can’t resolve it because we have a mess. I’ll recommend to get code classes again.

This wouldn’t work for OP’s problem - and it would be just as much of a “mess” in the code. While endlessly typing .Parent until you reach a player object is bad practice, game.Players.x will never work for determining the player who activated a Gui button.