Attempt to index nil with GetRankInGroup

Basically, what I’m trying to do is see if the owner of the game has a groupID below or above 50. If they don’t, it prints “No” and if they do, it prints “Yes”. However since GetRankInGroup doesn’t work with UserIDs (specifically the CreatorID) I had to use GetPlayerByUserID. Now when the thing runs, it says attempt to indexnil with GetRankInGroup.

local groupID = 12914986
local ownerID = game.CreatorId
local MinRank = 50
local players = game:GetService("Players")
local plr = players:GetPlayerByUserId(ownerID)


	if plr:GetRankInGroup(groupID) >= 50 then
		print("No")
		
	elseif plr:GetRankInGroup(groupID) <= 50 then
		print("Yes")
	
	end

2 Likes

It prints No if rank is higher than 50, is tht intended?

local groupID = 12914986
local ownerID = game.CreatorId
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	if player.UserId == ownerID then
		if player:GetRankInGroup(groupID) >= 50 then
			print("Rank greater than or equal to 50.")
		elseif player:GetRankInGroup(groupID) < 50 then
			print("Rank less than 50.")
		end
	end
end)

Oh yeah that’s a mistake on my part

However wt the issue exactly? Cuz rn i hav no idea abt it.

That would work but my client said no PlayerAdded since that’s not what he was looking for or something

game:GetService("Players") isnt sufficient use GetPlayers

Try using a for loop on the players val.

Instead do:

for i, player in pairs(players:GetPlayers()) do
    local plr = player:GetPlayerByUserId(ownerID)
    if plr:GetRankInGroup(groupID) >= 50 then
		print("No")
		
	elseif plr:GetRankInGroup(groupID) <= 50 then
		print("Yes")
	
	end
end

Local script or server script?

local groupID = 12914986
local players = game:GetService("Players")
local player = players.LocalPlayer
local ownerID = game.CreatorId

if player.UserId == ownerID then
	if player:GetRankInGroup(groupID) >= 50 then
		print("Rank greater than or equal to 50.")
	elseif player:GetRankInGroup(groupID) < 50 then
		print("Rank less than 50.")
	end
end

This would be a local script.

I am sure that it requires you to be in game, if you are not in game or the script runs before you load in it will simply error because player instance for “owner” does not exist.

Yeah PlayerAdded doesn’t really work in LocalScript because the LocalScript is loaded after the player is already in game but that’s pretty self explanatory.