# setRank

#### api.setRank

`api.setRank(userid: string or number, rank: string or number)`: table

| Argument | Type             | Description                                              |
| -------- | ---------------- | -------------------------------------------------------- |
| userid   | string or number | The roblox players userid                                |
| rank     | string or number | The rank id or rank name you'd like to rank the user to. |

Returns a table including status and rank data.

#### Return table

```lua
{
  status = boolean,
  code = number,
  error = string,
  data = {
    id = number,
    name = string,
    rank = number,
    memberCount = number
  }
}
```

| Table            | Type    | Description                                                            |
| ---------------- | ------- | ---------------------------------------------------------------------- |
| status           | boolean | True if request went though, False if request didn't go through        |
| code             | number  | The status code of the request                                         |
| error            | string  | This tells you what went wrong. Only will be given if status is false. |
| data.id          | number  | rolesetid of users new rank                                            |
| data.name        | string  | Name of users new rank                                                 |
| data.rank        | number  | rankid of users new rank                                               |
| data.membercount | number  | How many users are in the new rank                                     |

#### Possible Errors

```lua
{
  status = false,
  code = 429,
  error = "Too Many Requests"
}
{
  status = false,
  code = 400,
  error = "You cannot change the user's role to the same role"
}
{
  status = false,
  code = 403,
  error = "The bot does not have permissions to rank this user"
}
{
  status = false,
  code = 404,
  error = "Role not found with provided query"
}
{
  status = false,
  code = 402,
  error = "The user you provided is not in the group"
}
{
  status = false,
  code = 405,
  error =  "The user is invalid or does not exist"
}
{
  status = false,
  code = 406,
  error = "There are two or more roles with the rank 1. You must specify the role name"
}
{
  status = false,
  code = 0,
  error = "Error not found"
}
```

#### Example

```lua
local API = require(10005387312)({key = "Key Here", beta = false}) 

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:Connect(function()
		if player.UserId == 123445 then
			local success, rank = pcall(function()
				return API.setRank(player.UserId, 2) -- Example on how to rank on id
				--return API.setRank(player.UserId, 'test') -- Example on how to rank on name
			end)
			if not success then return error(rank) end
			if rank.status then
				print(player.Name .. ' was ranked to ' ..rank.data.name)
			else
				error('EasyRanks '.. tostring(rank.code) .. ': ' .. rank.error)
			end
		end	
	end)
end)
```

This example ranks someone with the userid 123445 to rankid 2. This shows you how to rank someone when they join your game, hopefully you can take this and make more out of it.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.easyranks.tech/reference/api-reference/setrank.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
