Player is returning nil

Hey, I’m working on an admin panel script but am having an issue. I’m trying to see if the player owns a gamepass in my game. The output keeps saying I can’t index nil with “Name.”

Note: ChosenPlayer is a string value in repStorage. It keeps track of which player the admin has selected from the player list. The name and value of the stringvalue then becomes the player’s name.

Script:

local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local checkEvent = replicatedStorage:WaitForChild("CheckGPEvent")
local MarketplaceService = game:GetService("MarketplaceService")

local chosenPlayer = replicatedStorage:WaitForChild("ChosenPlayer")

local passId = 228660484




local function onRemoteReceive(value)

	local value2
	print(chosenPlayer.Value)

	
	print("Server began")
	wait(1)
	if MarketplaceService:UserOwnsGamePassAsync(players:FindFirstChild(chosenPlayer.Name).UserId, passId) then
		print("Player has gamepass")
		value2 = "Yes"
		print(value2)
		checkEvent:FireClient(players:FindFirstChild(chosenPlayer.Name), value2)
	else
		print("Player has gamepass")
		value2 = "No"
		checkEvent:FireClient(players:FindFirstChild(chosenPlayer.Name), value2)
		
	end
end

checkEvent.OnServerEvent:Connect(onRemoteReceive)
4 Likes

here try mine players[chosenPlayer.Value].UserId

You can get the player ID from the name by using GetUserIdFromNameAsync (“UserName”)

local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local checkEvent = replicatedStorage:WaitForChild("CheckGPEvent")
local MarketplaceService = game:GetService("MarketplaceService")

local chosenPlayer = replicatedStorage:WaitForChild("ChosenPlayer")

local passId = 228660484




local function onRemoteReceive(value)

	local value2
	print(chosenPlayer.Value)
    local playerID = players:GetUserIdFromNameAsync(chosenPlayer.Value)

	print("Server began")
	wait(1)
	if MarketplaceService:UserOwnsGamePassAsync(playerID , passId) then
		print("Player has gamepass")
		value2 = "Yes"
		print(value2)
		checkEvent:FireClient(players:getPlayerByUserId(playerID ), value2)
	else
		print("Player has gamepass")
		value2 = "No"
		checkEvent:FireClient(players:getPlayerByUserId(playerID ), value2)
		
	end
end

checkEvent.OnServerEvent:Connect(onRemoteReceive)

This should work it uses the “ChosenPlayer” value

2 Likes

No, it doesn’t work. It still says the player is nil

1 Like

I updated the code but Are you sure that “ChosenPlayer” Value in set to a username

1 Like

100% positive. It is definitely a username

2 Likes

You’re code doesn’t work btw. The script doesn’t recognize “getPlayerByUserId” or “GetUserIdFromNameAsync”

There are orange lines under all these things

1 Like

It’s meant to be game:GetService("Players"):GetUserIdFromNameAsync(<value-here>). This is the full function.

1 Like

I apologize for not remembering to use the player’s service. It must be
players:GetUserIdFromNameAsync(chosenPlayer.Value)
and
players:getPlayerByUserId(playerID)

2 Likes

Output:

Players:GetUserIdFromNameAsync() failed: Unknown user

1 Like

I see there’s a wait(1) in there - make sure chosenPlayer is not destroyed during the wait

3 Likes

chosenPlayer is not destroyed during the wait

1 Like

I’m assuming the print(chosenPlayer.Value) works fine? If so I would try setting a variable equal to chosenPlayer.Value outside of the if statements, and using that variable instead of chosenPlayer.Name

1 Like

Show us the code that is setting the ChosenPlayer’s value.

1 Like

It seems as if you are changing the StringValue on the client side, which would explain why the server sees it as nil, because it only knows what the server knows, not the client. You should keep it this way(if you have it change on the server, it could cause issues when multiple people are using the admin system).
You should only change the Value property, not the Name property of the StringValue everytime someone new is selected(unless you have some unknown reason for it).

What I can recommend to fix this issue(if it is the issue) is:
You should send who the admin has selected through the remote event, and have your remote event process it accordingly.

1 Like

Oh I see, change ChosenPlayer.Name to ChosenPlayer.Value

1 Like

Sorry for the late response, the issue was something other than that, but I got figured it out. Thanks for the help though

Sorry, been offline for a bit. I was changing the string value on the server actually. This was of course problematic, like you said, so I switched it over to the client. I made some changes to the script and it works fine now. Thank you for the help

1 Like