# Examples

Here you can find a few examples of how to use the API. Not all examples are confirmed to work, but they are examples to show how to use the API. We will not provide any support on fixing or modifying these examples, so please do not make a ticket asking staff to create or modify any scripts you make; however, you may ask for support on why your script might not be working.

#### Ranking Commands:

```lua
local API = require(10005387312)({key = "Key Here", beta = false}) 
local groupId = 1234;
local requiredRank = 14; -- group role id+ that can use the commands

function slice(tbl, first, last, step)
	local sliced = {}

	for i = first or 1, last or #tbl, step or 1 do
		sliced[#sliced+1] = tbl[i]
	end

	return sliced
end


game.Players.PlayerAdded:connect(function(player)
	player.Chatted:connect(function(msg)
		local args = msg:split(" ")
		if args[1] == ":setrank" then
			local user = args[2]
			local rank = slice(args, 3)
			rank = tonumber(table.concat(rank, ' '))
			if user and rank then
				if game:GetService("Players"):FindFirstChild(user) then
					if player:GetRankInGroup(groupId) >= requiredRank then
						local success, rank = pcall(function()
							return API.setRank(game.Players[user].UserId, rank) -- Example on how to rank on id
						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
		elseif args[1] == ":promote" then
			local user = args[2]
			if user then
				if game:GetService("Players"):FindFirstChild(user) then
					if player:GetRankInGroup(groupId) >= requiredRank then
						local success, rank = pcall(function()
							return API.promote(game.Players[user].UserId)
						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
		elseif args[1] == ":demote" then
			local user = args[2]
			if user then
				if game:GetService("Players"):FindFirstChild(user) then
					if player:GetRankInGroup(groupId) >= requiredRank then
						local success, rank = pcall(function()
							return API.demote(game.Players[user].UserId)
						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
		elseif args[1] == ":shout" then
			table.remove(args, 1)
			local msg = table.concat(args, ' ')
			print(msg)
			if msg then
				if player:GetRankInGroup(groupId) >= requiredRank then
					local success, shout = pcall(function()
						return API.shout(msg)
					end)
					if not success then return error(shout) end
					if shout.status then
						print('The group shout has been changed to ' .. shout.data.body)
					else
						error('EasyRanks '.. tostring(shout.code) .. ': ' .. shout.error)
					end
				end
			end
		end
	end)
end)
```

This script allows groups to have commands in game that can allow them to use our API functions to manage their group. This script contains the following commands: setrank, promote, demote, and shout.

#### Automatic Points Management:

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

local Config = {
	['GroupId'] = 00;
	['PointsName'] = 'Points'; --This should be the name of your points. Ex: 'Worker Points'
	['MinRank'] = 3; --This is the rank someone has to be above in order for the system to watch for their points.
	['Ranks'] = {
		[200] = { --The 200 represents the number of points required for this rank. If your point value is a string, use a string.
			['Rank'] = 3 --The 2 represents the rank they player will get if they have the required amount of points.
		};
		[500] = {
			['Rank'] = 4
		};
	};
};

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Player:WaitForChild("leaderstats")
	local Points = Leaderstats:WaitForChild(Config.PointsName)
	local StartingRank = Player:GetRankInGroup(Config.GroupId)
	local Info = Config.Ranks[Points.Value]

	if Info then
		if StartingRank >= Config.MinRank and StartingRank < Info.Rank then
			local success, rank = pcall(function()
				return API.setRank(Player.UserId, Info.Rank) -- Example on how to rank on id
			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

	Points.Changed:Connect(function(Change)
		local NewVal = Points.Value
		local Info1 = Config.Ranks[NewVal]
		if Info1 then
			local Rank = Player:GetRankInGroup(Config.GroupId)
			if Rank >= Config.MinRank and Rank < Info1.Rank then
				local success, rank = pcall(function()
					return API.setRank(Player.UserId, Info1.Rank) -- Example on how to rank on id
				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)
end)
```

This script allows groups to rank users whenever they reach a certain number of points in game. This script uses the leaderboard stats (server side) to check how many points they have and rank them accordingly. This example was used for CheckMeIn CMI points.

{% hint style="warning" %}
More examples will be added later on, you can always check this game out for more examples in studio.\
\
[UNCOPYLOCKED GAME](https://www.roblox.com/games/10210318855/EasyRanks-API)
{% endhint %}


---

# 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/overview/examples.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.
