Kick Message That Says The Players Rank In Group

I’m trying to create a script for an application center that kicks the player if they are not a specific rank in the group. The script works but I want to add more to the kick message, I’m trying to make it so that the kick message tells the player their rank in the group but I don’t know how to get the group rank ID into the name of the group rank.

What it currently looks like:
Screenshot 2022-08-17 122531

What I want it to look like:

Script:

local plr = game:GetService("Players")
local groupId = 10074000

plr.PlayerAdded:Connect(function(player)
	wait(0.5)
	if player:GetRankInGroup(groupId) == 2 then
		script:Destroy()
	else
		player:Kick("This application is only available to Clearance Level 0, your current rank is " ..player:GetRankInGroup(groupId)..".")
	end
end)

I am aware I don’t need this, I just want to learn how it would be done since I’m still learning Lua.

1 Like

You can just use :GetRoleInGroup to get the string of their role name:

--//Services
local Players = game:GetService("Players")

--//Controls
local GroupId = 10074000

--//Functions
Players.PlayerAdded:Connect(function(player)
	task.wait(0.5)
	
	local playerRank = nil

	local success, errorMessage = pcall(function()
		playerRank = player:GetRoleInGroup(GroupId)
	end)

	if not success or not playerRank then
		warn(errorMessage)

		player:Kick("An error has occurred, please join later.")

		return
	end
	
	if playerRank == 2 then
		script:Destroy()
	else
		player:Kick("This application is only available to Clearance Level 0, your current rank is ".. player:GetRoleInGroup(GroupId) ..".")
	end
end)

Or if you’re looking for custom titles, you can try this:

--//Services
local Players = game:GetService("Players")

--//Controls
local GroupId = 10074000

--//Tables
local RoleTitles = {
	[6] = "random guy",
	[255] = "cool guy",
}

--//Functions
Players.PlayerAdded:Connect(function(player)
	task.wait(0.5)
	
	local playerRank = nil
	
	local success, errorMessage = pcall(function()
		playerRank = player:GetRoleInGroup(GroupId)
	end)
	
	if not success or not playerRank then
		warn(errorMessage)
		
		player:Kick("An error has occurred, please join later.")
		
		return
	end
	
	if playerRank == 2 then
		script:Destroy()
	else
		local roleName = RoleTitles[playerRank]
		player:Kick("This application is only available to Clearance Level 0, your current rank is ".. roleName ..".")
	end
end)

I suggest wrapping those calls in a pcall, to prevent issues when the web is down.

local success,result = pcall(function()
    return player:GetRoleInGroup(GroupId)
end)

if success then
    if result == 2 then
       script:Destroy()
    else
       local roleName = RoleTitles[result]
       player:Kick("This application is only available to Clearance Level 0, your current rank is ".. roleName ..".")
    end
end

That was WAY easier than I thought it was lol

Thanks for the second script too that is what I was trying to do but I wasnt putting the numbers in [ ] so it gave an error.

No problem. If you have any more questions, feel free to ask.

1 Like

Thanks for telling me, I just changed the scripts.

1 Like