Issues with GetRankInGroup

Hello everyone, I’m trying to make an automatic gate that opens only when a specific player touches it. I’m struggling with the script because I can’t understand what I’m doing wrong:

local team = game.Teams:WaitForChild("Police")
local idist = 11044391 -- group id of the team
local id = 7865740 -- group id for the admins

script.Parent.Regione.Touched:Connect(function(hit)
	local human = hit.Parent:FindFirstChild("Humanoid")
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player and player.Team == team and player:GetRankInGroup(idist) >= 1 or player:GetRankInGroup(id) >= 254 then
		print("it works")
	end
end)

player.Team works fine but the issues are in player:GetRankInGroup(idist) >= 1 or player:GetRankInGroup(id) >= 254.

I’m getting this error:
https://gyazo.com/ed1843b037957218aaafef08488f1802

Can someone please help me?

your code happens if anything touches the part,[not always the humanoid] try this:

local human = hit.Parent:FindFirstChild("Humanoid")
if human then
 local player = game.Players:GetPlayerFromCharacter(hit.Parent)

and then you put an extra “end”

It needs to be GetRoleInGroup instead of GetRankInGroup

Check out this page:
https://developer.roblox.com/en-us/api-reference/function/Player/GetRoleInGroup

The problem may be related to the way your code “translates” the if statement. Try prioritizing the or check using brackets:
... and (player:GetRankInGroup(idist) >= 1 or player:GetRankInGroup(id) >= 254) then
This is probably the case, if writing print(player) one line above returns nil when the error occurs(because the part was touched by a random non-character part).

Tried putting

if player and player.Team == team and (player:GetRankInGroup(idist) >= 1 or player:GetRankInGroup(id) >= 254) then

and it seems to work, I’m testing it right now.

1 Like

It worked, thank you all for helping!