Retriving the player that clicked

I have tried to refrence the player that clicked my gui in studio but have failed, please tell me how to refrence it if possible.

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

local user = script.Parent.TextBox.Text
local item = player.Character:FindFirstChildOfClass(“Tool”)
local cloneditem = item:Clone()
cloneditem.Parent = game.Players(user).BackPack

end)
The player returns as nil.

1 Like

If this is a server script, you shouldn’t be manipulating UI on the server. This is a bad practice and is what LocalScripts are for. Just use a LocalScript instead, and fire a RemoteEvent when they click. Then on the server, it will say which player it was and you can use that to give them the tool.

Edit: If this is already a local script, the part where you give them the tool won’t replicate to other clients. Same solution as above; use RemoteEvents.

3 Likes

Studio as in a plugin or something? either way you cannot put player as argument in a MouseButton1Click event, they don’t return the player that clicked, they return nothing (nil)

1 Like

If you’re using a local script you can get the player

local player = game.Players.LocalPlayer

script.Parent.TextButton.MouseButton1Click:Connect(function()
   print (player.Name)
end)

and if you want to change something on the server you can use a remote event.

Congrats on your first devforum post!
Just a few tips to add here:

  1. Make sure to mark the solution.
  2. Mark code blocks like this:
print("Your Code here")

```lua
print(“Your Code here”)
```
3) Use game.Players.LocalPlayer
@roasthistorychicken stole what I was about to say. : /

3 Likes