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

How do I get a players ID from their username?

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)

4 comments

0

why are you trying to find a Player inside of a ClickDetector...?

0

you cannot just make up and create arguments for a function; they are predetermined.

0

It is not a click dectector, this is located in a GUI.

0

oh then still, it's a different argument

2 answers

✓ Accepted answer
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.

3 comments

0

ur formatting was not correct; write code inside of the ~'s.

0

It is.

0

Thank you so much, it worked!

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)

1 comment

0

Thank you so much for trying to help!

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