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)
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)
I apologize for not remembering to use the player’s service. It must be
players:GetUserIdFromNameAsync(chosenPlayer.Value)
and
players:getPlayerByUserId(playerID)
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
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.
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