Attempt to index nil with GetRoleInGroup?

My code was always working before, After a month i came back to an old project and it gave me an error i never had seen before.

It is inside a normal script for reasons.
The code may be a bit messy but i just don’t understand it.

local Character = OverheadGuiObject.Parent
local plr = game:GetService("Players"):GetPlayerFromCharacter(Character)

local GroupID = 7628430
local GetRankInGroup = plr:GetRoleInGroup(GroupID) -- This line.
local InGroup = plr:IsInGroup(GroupID) -- Also this line.
1 Like

Your issue could be that the script is running before the character itself has loaded. You can approach this with a few different methods.

.1) Simply add a task.wait(1) (Not the most reliable)

.2) Use a loop:

local plr = game:GetService("Players"):GetPlayerFromCharacter(Character)
while not plr do
    Character = OverheadGuiObject.Parent
    plr = game:GetService("Players"):GetPlayerFromCharacter(Character)
    task.wait(.1)
end

This will keep attempting to get a player based off OverheadGuiObject’s Parent by constantly updating the variables till it exists. So this could be an infinite yield theoretically.

Sorry for the late reply, I was at work + bed, But i still get the same error.

Players:GetPlayerFromCharacter () only return if found
try printing the result

Like this? It prints my Roblox userName

local plr = game:GetService("Players"):GetPlayerFromCharacter(Character)
print(plr)
BobbieTrooper  -  Server - OverheadGui:12

is the Character nil?
if its nil then the plr will be nil

I don’t think its nil. The script is inside StarterCharacterScripts and will only run if the character has loaded in.

1 Like

Is OverheadGuiObject nil before indexing?

If its inside StarterCharacterScripts you Can get the plr by game.Players.LocalPlayer

local plr = game:GetService("Players").LocalPlayer
local Character = plr.Character

local GroupID = 7628430
local GetRankInGroup = plr:GetRoleInGroup(GroupID) -- This line.
local InGroup = plr:IsInGroup(GroupID) -- Also this line.

The script is a normal script, not a LocalScript.

Can you show the Entire Script?
Like the Part which Parents the OverheadGuiObject and stuff too.

Oh yes sorry, I havent checked this in a while. One moment.

--// Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

--// Variables
local GroupRoles = require(ReplicatedStorage.Modules.Group.Ranks)
local OverheadGuiObject = script.Parent
local PlrNameText = OverheadGuiObject.PlayerName

local Character = OverheadGuiObject.Parent
local plr = game:GetService("Players"):GetPlayerFromCharacter(Character)
print(plr)

local GroupID = 7628430
local GetRankInGroup = plr:GetRoleInGroup(GroupID)
local InGroup = plr:IsInGroup(GroupID)

--// Functions
OverheadGuiObject.Parent = Character.Head
PlrNameText.RichText = true 
PlrNameText.TextColor3 = Color3.fromRGB(255, 255, 255)

if InGroup then
	PlrNameText.Text = GroupRoles.Overhead_GroupRanks()[GetRankInGroup] .. plr.Name
else
	PlrNameText.Text = [[<font color="rgb(238, 196, 182)"> [Guest] </font>]] .. plr.Name
end

local Humanoid = Character:FindFirstChildOfClass("Humanoid")
Humanoid.NameDisplayDistance = 0

while RunService.Heartbeat:Wait() do
	for i, v in pairs(OverheadGuiObject:GetChildren()) do
		local success, err = pcall(function()
			if v.Name ~= "Player" and v.ClassName == "TextLabel" then
				v.Font = "Bodoni"
				local Stats = plr:WaitForChild("Stats")
				if v.Name == "Cash" then
					v.Text = v.Name .. ": $" .. Stats.Cash.Value
				end
			end
		end)
	end
end
local success, error = pcall(function()
    local Character = OverheadGuiObject.Parent
    local plr = game:GetService("Players"):GetPlayerFromCharacter(Character)

    local GroupID = 7628430
    local Role = plr:GetRoleInGroup(GroupID) -- This line.

    print(plr.Name, GroupID, Role)
end)

if not success then
    warn(error)
end

I’m printing all your data, if it doesn’t print, something is wrong with your GroupID. Also, IsInGroup should mostly be used for if statements!

It was due to a stupid mistake, Still thanks for everyones time. My bad