Get UserID of player that clicked brick

Im trying to make it that when someone clicks a brick it prints their userid. This is in a local script. Whats wrong with it.

script.Parent.MouseButton1Click:Connect(function() 
local player = game.Players.LocalPlayer 
print(player.UserId)
end)
1 Like

UserId should be userId instead, you just made a typo.

Stil no. Doesnt print anything.

Maybe it’s a locating issue then. Have you checked the output? Maybe script.Parent isn’t an actual button, or it isn’t the button you’re clicking to test userId on.

I assume you mean a Part - which does not have an event called MouseButton1Click. You should consider using a ClickDetector for this: ClickDetector | Documentation - Roblox Creator Hub. You will also want to use a Server Script for this.

Check out this page for more help: ClickDetector | Documentation - Roblox Creator Hub

Its a brick not a button @Dracolyx

As said above insert a ClickDetector into the part with a MouseClick Event Instead of MouseButton1Click. (put the script inside of the clickdetector)

So this?

function onMouseClicked()
local player = game.Players.LocalPlayer 
print(player.userId)
script.Parent.ClickDetector.MouseClick:connect(onMouseClick)

Well, for starters this should be handled in a server script since I assume you want a player to be able to click a part inside of workspace. The way you can do this is by using a ClickDetector. What you provided was a MouseButton1Click function for a ButtonGui element.

local ClickDetector =  -- Define

ClickDetector.MouseClick:Connect(function() -- Add player argument
  -- Do things

end)

Yeah, but don’t forget to add an end for the onMouseClicked() function. Also you don’t need to type the player variable as it’s a parameter of the event.

You forgot to add an end in there, so be careful. However, please, oh please…

INDENT YOUR CODE.

1 Like

Usually on devforums it isn’t a big deal to indent or not, because you can’t when typing freestyle in the chatbox. But you should probably indent the code in studio, before pasting into here.

You are forgetting an end to your function. When you run your function it will error because you can’t get the LocalPlayer from a server script, which is why there is a Player argument to see what player has clicked the instance.

userId is wrong and was deprecated a long time ago; you should be using UserId. Please don’t spread misinformation.

5 Likes