Problem with a script to save a player's UserId in a value

This is the code, in principle I do not see that the UserId of the local player is saved in the value of: ReplicatedStorage.Players.HitmanCrime.Player

The script is in a LocalScript located inside a ProximityPrompt.

In principle, the error is that when activating the ProximityPrompt, the value of the UserId of the local player is not saved.

local proximityPrompt = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local l = ReplicatedStorage.Players.HitmanCrime

local localPlayer = Players.LocalPlayer
local player1 = l.Player

proximityPrompt.Triggered:Connect(function()
	player1.Value = localPlayer.UserId
end)
2 Likes

I’m pretty sure the ProximityPrompt is inside Workspace, and LocalScripts can’t run in there. Try moving it somewhere else

1 Like

I moved it to ServerScriptService, then tried putting it in StarterPlayerScripts, but it still doesn’t save:

local part = game.Workspace["Part to CRIME1"]
local proximityPrompt = part.ProximityPrompt
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local l = ReplicatedStorage.Players.HitmanCrime

local localPlayer = Players.LocalPlayer
local player1 = l.Player

proximityPrompt.Triggered:Connect(function()
	player1.Value = localPlayer.UserId
end)

It’s a quite easily solution i think based on the details you gave. Modifying a value of an int value object will only keep it like that during game session. or while the player is inside the game. These values only good for some temporarly value holder. if you want to save data for longer periods or for later on usages then use datastores instead.

  1. Make sure that the ReplicatedStorage.Players.HitmanCrime.Player value exists and is a StringValue or IntValue type. If it doesn’t exist, you can create it using Instance.new(“StringValue”) or Instance.new(“IntValue”)
  2. Verify that the part variable is correctly referencing the part in your game that has the ProximityPrompt. You can do this by printing the part variable to the output and checking if it is the expected part
  3. Check if the proximityPrompt object has the Triggered event connected to the provided function. You can do this by printing the proximityPrompt.Triggered event to the output and checking if it is connected to the function.
  4. Ensure that the local player variable is correctly referencing the local player. You can print the localPlayer variable to the output and check if it is the expected Player object.

btw,you used Players.LocalPlayer in a localscript if I understood ok, right?

You can use LocalPlayer ONLY IN A LOCALSCRIPT

I tried to do a debug, printing a message when it is activated, but it didn’t work, I tried it with the localscript located in ServerScriptService.

1 Like

you cant use localscripts in SERVERSCRIPSERVICE,like the name says, SERVERSCRIPTSERVICE

Localscripts works ONLY for client,and Script for all clients,try putting the code in a localscript in a StarterGUI,StarterPlayeScripts,etc. but NOT Workpspace ServerScriptService or ReplicatedFirst/Storage

Putting the Script in StarterPlayerScripts still doesn’t work.

or just put Players.userid instead of player.UserId,and put it in a script as child of the proximityprompt

Workspace.Part to CRIME1.ProximityPrompt.Script:12: attempt to index nil with ‘UserId’

local proximityPrompt = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local l = ReplicatedStorage.Players.HitmanCrime

local localPlayer = Players.LocalPlayer
local player1 = l.Player

proximityPrompt.Triggered:Connect(function()
	player1.Value = localPlayer.UserId
	print("Proximity Found")
end)

1 Like

The error means that LocalPlayer doesn’t exist. You can’t get LocalPlayer on a server script, instead get the player through the Triggered event:

proximityPrompt.Triggered:Connect(function(playerTriggered) -- player who triggered the prompt
	player1.Value = playerTriggered.UserId
	print("Proximity Found")
end)
1 Like

here go, my solution looks like this:

-- you must put where the ProximityPrompt is.
local Part = workspace:WaitForChild("Part")
local proximityPrompt = Part:WaitForChild("ProximityPrompt")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer

local l = ReplicatedStorage:WaitForChild("Players"):WaitForChild("HitmanCrime")
local player1 = l.Player -- I guess this is a number value right?

proximityPrompt.Triggered:Connect(function()
	player1.Value = localPlayer.UserId
	print("Proximity Found")
end)

I hope it helps you.

Answer this questions to me (if is not what you want):

  • where this script should be?
  • this script is server or client side?
  • you have SURE the value is a NumberValue?
1 Like

In the Explorer go to your script, click on it once then go to properties and set RunContext to Client instead of Legacy

Because the local ReplicatedStorage = game:GetService("ReplicatedStorage") looks like a Client script but client scripts localScripts cant run in Workspace.

This is what i think.

15:49:19.834 Proximity Found - Cliente - LocalScript:13
15:49:19.849 Workspace.Part to CRIME1.ProximityPrompt.Script:12: attempt to index nil with ‘UserId’

In the error they provided I noticed the script is named Script so I think that they’re using a server Script by mistake instead of a LocalScript, and LocalPlayer is nil when used in a server script

2 Likes

make sure you are inserting this script where the image shows:
image
And make sure is a LocalScript
image
Server-side scripts can’t use LocalPlayer.

Since Proximity Found is successfully being printed then the error is happening in a different script inside the ProximityPrompt

What Johhny* said looks like true i tried the script and worked


check for others scripts errors. And check if the NumberValue is filled with your ID.

(this can happens because you may copy and pasted the same script or something like that.)

2 Likes