Script doesnt detect Players ID, what to do?

Hello! I am currently new to scripting and facing Problems. I am working on a script that gets the Players UserID and copies the ID into a Stringvalue, I tried making it but it gives me errors.

The Error says “attempt to index nil with ‘UserId’”

Ive tried looking on scriptinghelpers but it wasnt very helpful since it doesnt have alot of users online.

Also the StringValue is parented to StarterCharacterScripts as a normal Script, image . This is the Code I made.

ID = script.Parent.Value

Player = game.Players.LocalPlayer.UserId

script.Parent.Value = Player

If somebody could tell me what I did wrong it would be very helpful since I am stuck with this problem now for over half a hour.

(Update, I have tried with a local script, it doesnt give any errors now (yay) but it still doesnt do anything)

When using scripts in the StarterCharacter or StarterPlayer folder the script must be a local script to work. All you have to do is replace the script with a local script. Don’t forget to mark this post as a solution if it helped! :slight_smile:

1 Like

Thanks for the reply! Ive made it a localscript and it doesnt give errors anymore, but it still doesnt work.

Try this as a LocalScript:

local UserId = script.Parent.Value
local Players = game:GetService("Players")

UserId = Players.LocalPlayer.UserId
1 Like

yo, the UserId variable holds a boolean now.

1 Like

Where did you put the “UserId” value? Is it on ReplicatedStorage or Workspace or other?

1 Like

Try this in a local script

print( game.Players.LocalPlayer.UserId )

1 Like

If on the server, Players.LocalPlayer will return nil, since the LocalPlayer is, well, local. You have to run the script on a LocalScript.

1 Like

That’s because it’s a Script, so it’s not on the client, therefore you won’t be able to define .LocalPlayer since it’s in the server. Do you mean something like this?

game.Players.PlayerAdded:Connect(function(player)
    local stringValue = Instance.new("StringValue", player)
    stringValue.Value = player.UserId
end)
4 Likes

The UserId Value is in the StarterCharacterScripts.

2 Likes

The Problem was that I was using a Stringvalue and not a numbervalue, I fixed it and thanks everybody for your help!

Did your script work now? Please mark the post with the solution

1 Like

I managed to fix it by making the Stringvalue into a Numbervalue, thanks for your help tho!

I messaged a friend and he told me the problem.

The problem is fixed now but it was in StarterCharacterScripts.

it’s ok @NEMOTASTlC we are always here to help. but, Always mark your topic as solved

2 Likes

I managed to fix it by making the Stringvalue a Numbervalue.

Try

ID = script.Parent
Player = game.Players.PlayerAdded:Wait()
ID.Value = Player.UserId
1 Like

Oh my god, It’s solved. @NEMOTASTlC please mark it as solved.

1 Like