Attempt to index nil with 'GetRankInGroup'

Not sure why this error comes up after I made a group rank door. If I used game.players.playeradded, it would be a mess. If a player once joined who isn’t in the group that was required, the group rank door automatically sets its cancollide to true.

Script: (Server Script)

local GroupId = 12648334
local RankThatCanPass = 1

local HQ = 12648144
local HQThatCanGoThrough = 250
local plr = game.Players.LocalPlayer 

script.Parent.Touched:Connect(function()
	if plr:GetRankInGroup(GroupId) >= RankThatCanPass or plr:GetRankInGroup(HQ) >= HQThatCanGoThrough then
		print("Player is in group")
	wait(0.1)
	script.Parent.CanCollide = false
	script.Parent.BrickColor = BrickColor.new("Institutional white")
	wait(1.2)
	script.Parent.CanCollide = true
		script.Parent.BrickColor = BrickColor.new("Really blue")
	else
	 	print("Player is not in group")
	end
end)

I have no time to test this. tell me if this works. (never used GetRankInGroup() lol)

local GroupId = 12648334
local RankThatCanPass = 1

local HQ = 12648144
local HQThatCanGoThrough = 250

script.Parent.Touched:Connect(function(otherPart)
	local char = otherPart.Parent
	local player = game.Players:GetPlayerFromCharacter(char)
	if player then
		if player:GetRankInGroup(GroupId) >= RankThatCanPass or player:GetRankInGroup(HQ) >= HQThatCanGoThrough then
			print("Player is in group")
			wait(0.1)
			script.Parent.CanCollide = false
			script.Parent.BrickColor = BrickColor.new("Institutional white")
			wait(1.2)
			script.Parent.CanCollide = true
			script.Parent.BrickColor = BrickColor.new("Really blue")
		else
			print("Player is not in group")
		end
	end
end)

Oh and you cannot use game.Players.LocalPlayer inside a Server Script!

1 Like

Alright, thanks. Can I ask just a little dumb question? What is the touch’s parent? The part?

no, the character.

30 funny yeah ey

wait do you mean like the “otherPart”?

1 Like

Yes, the otherpart function variable.

it’s the character of the player

Oh, alright. Thanks again, sorry for the late reply.

local Part = script.Parent

local GroupId = 12648334
local RankThatCanPass = 1
local HQ = 12648144
local HQThatCanGoThrough = 250

Part.Touched:Connect(function(Hit)
	local plr = game.Players:GetPlayerFromCharacter(Hit.Parent)
	if plr then
		if plr:GetRankInGroup(GroupId) >= RankThatCanPass or plr:GetRankInGroup(HQ) >= HQThatCanGoThrough then
			print("Player is in group")
			task.wait(0.1)
			Part.CanCollide = false
			Part.BrickColor = BrickColor.new("Institutional white")
			task.wait(1.2)
			Part.CanCollide = true
			Part.BrickColor = BrickColor.new("Really blue")
		else
			print("Player is not in group")
		end
	end
end)

Tested and this works.

3 Likes

The problem is it’s trying to index an object on line 9 that returns nil. First of all, use task.wait() as it is a better version than the global wait. Second, make sure this is a LocalScript under StarterGui. Third, just use the first GetRankInGroup thing you stated, then for the “or”, take that, and make it an elseif statement. Idk what is wrong with or, but it just sometimes doesn’t work.