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

Getting UserId on Click?

Asked by
iNicklas 215 Moderation Voter
9 years ago
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

Log in to vote
2
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
9 years ago

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)
0
Omg thank you!, i just got a question, why is it (function(Player), you aren't defining Player anywhere? iNicklas 215 — 9y
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. Shawnyg 4330 — 9y
0
Oh, thats quite smart. ill keep that notified :) iNicklas 215 — 9y
Ad
Log in to vote
-1
Answered by
funyun 958 Moderation Voter
9 years ago

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)

Answer this question