How to get a player's role in a group?

I have a script which gives a player a chattag if they are a certain role in my group, However it has given me all the roles?

Is it because i’m the owner?

local plrs = game.Players
local sss = game.ServerScriptService
local chatService = require(sss:WaitForChild('ChatServiceRunner'):WaitForChild('ChatService'))
local groupID = 5336178

chatService.SpeakerAdded:Connect(function(plr)

	local speaker = chatService:GetSpeaker(plr)	

	-- COPY FROM THIS
	repeat wait() until
	plrs[plr].leaderstats:WaitForChild("ChatValues").Owner.Value == true
	if plrs[plr].leaderstats:WaitForChild("ChatValues").Owner.Value == true then
		speaker:SetExtraData('NameColor', Color3.fromRGB(85, 0, 255))
		speaker:SetExtraData('ChatColor', Color3.fromRGB(255, 255, 255))
		speaker:SetExtraData('Tags', {{TagText = 'Owner', TagColor = Color3.fromRGB(85, 255, 127)}})
		print("Gave the owner a special chat tag and chat color!")		
	end
	
	local RankCheck = plrs[plr]:GetRankInGroup(groupID)

	local GroupTagCheck = RankCheck
	
	if plrs[plr]:GetRankInGroup(253) then
		plrs[plr].leaderstats:WaitForChild("ChatValues").Staff.Value = true
	end
	
	repeat wait() until
	plrs[plr]:GetRankInGroup(253) and plrs[plr].leaderstats:WaitForChild("ChatValues").Staff.Value == true
	if plrs[plr]:GetRankInGroup(253) and plrs[plr].leaderstats:WaitForChild("ChatValues").Staff.Value == true then
		speaker:SetExtraData('Tags', {{TagText = 'Server Booster', TagColor = Color3.fromRGB(255, 85, 255)}})
		print("Gave the Staff Member a special chat tag and chat color!")
	end
	
	if plrs[plr]:GetRankInGroup(4) then
		plrs[plr].leaderstats:WaitForChild("ChatValues").Contributor.Value = true
	end
	
	repeat wait() until
	plrs[plr]:GetRankInGroup(4) and plrs[plr].leaderstats:WaitForChild("ChatValues").Contributor.Value == true
	if plrs[plr]:GetRankInGroup(4) and plrs[plr].leaderstats:WaitForChild("ChatValues").Contributor.Value == true then
		speaker:SetExtraData('Tags', {{TagText = 'Contributor', TagColor = Color3.fromRGB(85, 255, 255)}})
		print("Gave the Contributor a special chat tag and chat color!")		
	end
end)

Help would be appreciated thank you.

local Game = game
local Players = Game:GetService("Players")

local GroupId = 0 --Change this to the desired group's ID.

local function OnPlayerAdded(Player)
	local Success, Result = pcall(function() return Player:GetRoleInGroup(GroupId) end)
	print(Result) --Prints the player's role in the specified group.
end

Players.PlayerAdded:Connect(OnPlayerAdded)

Use GetRankInGroup() for the player’s group rank instead.

3 Likes
local Game = game
local Players = Game:GetService("Players")

local GroupId = 5336178

local function OnPlayerAdded(Player)
	local Success, Result = pcall(function() return Player:GetRankInGroup(5336178) == 4 end)
	print(Result) --Prints the player's role in the specified group.
	if Result == "Contributor" then
		Player.leaderstats:WaitForChild("ChatValues").Contributor.Value = true
	end
end

I’ve done this yet it still doesn’t seem to work and print’s false nstead would you know why?
Since I’ve put my alt as contributor in my group and the value of the rank is 4.


game.Players.PlayerAdded:Connect(function(plr)
	local Success, Result = pcall(function() 
		return plr:GetRoleInGroup(GroupId) 
	end)
	
	if Result == "Contributor" then
		plr.leaderstats:WaitForChild("ChatValues").Contributor.Value = true
	end
end)```

Alot of the replys can be simpler:

local groupID = 0
game.Players.LocalPlayer:GetRoleInGroup(groupID)