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

Getting UserId on Click?

local ID = game.Players.LocalPlayer.userId
local value = script.Parent.Parent.Parent.USERID

script.Parent.Parent.Parent.USERID.Value = ID

function onClicked()
	script.Parent.Text = value

end

script.Parent.Parent.Parent.ClickDetector:connect(onClicked)

Whats wrong with it :/

2 answers

✓ Accepted answer

ClickDetectors fire the MouseClick event which returns the Player. You can't exactly use a LocalScript, since anyone can click the button at any given moment. For the parameter for the function, you'd set the Player variable. On your line 7, you were setting the text equal to an Object, which is impossible. You need to add .Value. Here is your script fixed up, with more organization (Normal script):

script.Parent.Parent.Parent.ClickDetector.MouseClick:connect(function(Player)
	script.Parent.Text = Player.userId
end)

The use of those variables wasn't needed, unless you were using them in another script as well. If that's the case, then:

script.Parent.Parent.Parent.ClickDetector.MouseClick:connect(function(Player)
	script.Parent.Parent.Parent.USERID.Value = Player.userId
	script.Parent.Text = Player.userId
end)

3 comments

0

Omg thank you!, i just got a question, why is it (function(Player), you aren't defining Player anywhere?

0

@iNicklas, As I said in my question, the parameter within the function, is set as the variable for the Player. It can be anything. 'Noob', 'Abc', but it will always equal the player who clicked it.

0

Oh, thats quite smart. ill keep that notified :)

Think of it this way. Storing a variable is like taking a picture of someone. After the picture is taken, the person can move around all he wants, but he remains still in the picture. If you store how fast a part is moving at a given moment, then you have that one number that won't change, but the part's speed will. So, in this example, switch lines 2 and 4 around.

local ID = game.Players.LocalPlayer.userId
script.Parent.Parent.Parent.USERID.Value = ID

local value = script.Parent.Parent.Parent.USERID.Value --Gotta change USERID to USERID.Value

function onClicked()
    script.Parent.Text = value

end

script.Parent.Parent.Parent.ClickDetector:connect(onClicked)

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