New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

How do I get a players ID from their username?

Asked by 3 months ago

I am using a regular server script, which it can not get the users playerID.

script.Parent.MouseButton1Click:Connect(function(Player)
    local playerId = Player.UserId
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P1.Disabled = false
    wait(2)
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P1.Disabled = true
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P2.Disabled = false
    wait(2)
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P2.Disabled = true
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P3.Disabled = false
    wait(2)
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P3.Disabled = true
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P4.Disabled = false
    wait(2)
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P4.Disabled = true
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P5.Disabled = false
    wait(2)
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P5.Disabled = true
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P6.Disabled = false
    script.Parent.Parent.Confirm.BackgroundColor3 = Color3.new(0, 255, 0)

In the developer console it was saying "attempt to index local 'Player' (a nil value)

0
why are you trying to find a Player inside of a ClickDetector...? KDarren12 690 — 3mo
0
you cannot just make up and create arguments for a function; they are predetermined. KDarren12 690 — 3mo
0
It is not a click dectector, this is located in a GUI. DennisSense 21 — 3mo
0
oh then still, it's a different argument KDarren12 690 — 3mo

2 answers

Log in to vote
1
Answered by
Velsity 207 Moderation Voter
3 months ago
Edited 3 months ago
local player = game.Players.LocalPlayer
local ID = player.UserId

script.Parent.MouseButton1Click:Connect(function()
    print(ID)
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P1.Disabled = false
    wait(2)
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P1.Disabled = true
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P2.Disabled = false
    wait(2)
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P2.Disabled = true
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P3.Disabled = false
    wait(2)
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P3.Disabled = true
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P4.Disabled = false
    wait(2)
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P4.Disabled = true
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P5.Disabled = false
    wait(2)
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P5.Disabled = true
    script.Parent.Parent.No1.Value:WaitForChild('Eng').P6.Disabled = false
    script.Parent.Parent.Confirm.BackgroundColor3 = Color3.new(0, 255, 0)

If I understand you correctly, you wanna know the ID of a player.

This is also a localscript, if you wish to do anything on the server, use a remoteevent.

What I did was refer to the ID of the player, which is the localplayer. And print it everytime they click the GUI

Also, you can use GetUserIdFromNameAsync if you really wanna do it from the server side, but it's gonna be harder.

0
ur formatting was not correct; write code inside of the ~'s. KDarren12 690 — 3mo
0
It is. Velsity 207 — 3mo
0
Thank you so much, it worked! DennisSense 21 — 3mo
Ad
Log in to vote
0
Answered by 3 months ago

You are using a gui. Guis do not have any way of getting the player as they are supposed to be manage by LocalScripts, which can access game.Players.LocalPlayer. Use a remote event.

LocalScript (Parented to gui button):

script.Parent.MouseButton1Click:Connect(function()
    local remoteEvent = game.ReplicatedStorage.RemoteEvent -- Make a remote event and reference it here
remoteEvent:FireServer(script.Parent)
end)

ServerScript (Parented to gui button):

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, gui) -- Reference same remote event
    local playerId = player.UserId
    -- Type the rest of the code here (replace every script.Parent with gui)
end)
0
Thank you so much for trying to help! DennisSense 21 — 3mo

Answer this question