Promote/rank script doesn't want to work with role names instead of IDs

So, I’m trying to make the rank command work with the name of the roles and not the numbers. Can someone tell me what I did wrong, as it doesn’t work.
Here is the code:

local HttpService = game:GetService("HttpService")
local ReplicatedStorage = game.ReplicatedStorage
local MinRank = script.MinRank.Value
local prefix = ";"

local function FindPlr(plr)
	local foundPlr = nil
	local plrs = game.Players:GetPlayers()
	for i = 1, #plrs do
		local current = plrs[i]
		if string.lower(current.Name):sub(1, #plr) == string.lower(plr) then
			foundPlr = current.Name
			break
		end
	end
	return foundPlr
end


game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(msg)
		if player:GetRankInGroup("GROUP_ID") >= 2 then
		local LowerCaseMessage = string.lower(msg)
				if string.find(LowerCaseMessage, ";passlowrank") then --Look for the rank command to be used
					local SplitMessage = string.split(msg, " ") --Split the message
					local full_player = FindPlr(SplitMessage[2]) --Get the 2nd word in the message
				local req_rank = SplitMessage[3]
				if req_rank:find('Constable') then
						local offrank = 6
				else if req_rank:find('Corporal') then
						local offrank = 7
					else if req_rank:find('Sergeant') then
						local offrank = 8
						else if req_rank:find('Lieutenant') then
						local offrank = 9
					--Get the 3rd word in the message
				if full_player == nil then 
						local playerInstance = game.Players:FindFirstChild(player.Name) --Get the player that sent the command.
						game.ReplicatedStorage.Notify:FireClient(playerInstance, "Uh-Oh","We couldn't find the player you wanted to rank!") --Notify the client that it failed.
					else --if player exsists
						local playerInstance = game.Players:FindFirstChild(player.Name) --Find player that sent command.
					game.ReplicatedStorage.Notify:FireClient(playerInstance, "Attempting.","Attempting to rank user...") --Send loading. 
					local Url = "URL-GOES-HERE"..offrank --Add RoleNumber To The Data sent to the WebServer.
						local success, response = pcall(function(data) --Pcall for obvious reasons
							local response = HttpService:GetAsync(Url) --Send request to webserver
							local data = HttpService:JSONDecode(response) --Decode Json Data.
						end)
						if success then --test if worked.
							game.ReplicatedStorage.Notify:FireClient(playerInstance, "Success!", ""..full_player.." has has passed the LR training, and has been ranked"..req_rank..".") --Custom edited the error messages.
						else
							game.ReplicatedStorage.Notify:FireClient(playerInstance, "UnSuccessful",""..full_player.." The command failed or the player is already a "..req_rank.." or higher rank.") --Custom edited the error messages.
						end
					end
					end
					end
					end
				end
			end
		end
	end)
end)

Did it work with ID’s?

Yes, it works with ID’s. But I want it to work with names.

Would it make a difference using the role names, though? I don’t know if this is possible to do with role names, but maybe you could add a note beside the script which lists what ID belongs to what role.

That’s what I tried to do. (It shows it in my code)

Can you try explaining to me on what you would like to achieve?

So, I’m trying to make the rank command rank someone using the name of the rank and not the number. E.g: ;rank randomuser Sergeant

Albright, I get you now.

Do you have any errors in output?


Feel free to @mention me, the forums are very busy right now, it’s easy to miss notifications.

Nope, there’s no errors.

local req_rank = SplitMessage[3]

After this line add the line:

print(req_rank)

Then show us what is says in output.

I really want to achieve this too!


Got this after adding

I did some tinkering and I’ve fixed it. Here’s the code for anyone who would like to use it :smile:

local HttpService = game:GetService("HttpService")
local ReplicatedStorage = game.ReplicatedStorage
local AppUrl = script.AppUrl.Value
local ApiKey = script.ApiKey.Value
local Groupid = script.GroupId.Value
local MinRank = script.MinRank.Value
local prefix = "!"

local function FindPlr(plr)
	local foundPlr = nil
	local plrs = game.Players:GetPlayers()
	for i = 1, #plrs do
		local current = plrs[i]
		if string.lower(current.Name):sub(1, #plr) == string.lower(plr) then
			foundPlr = current.Name
			break
		end
	end
	return foundPlr
end

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		if player:GetRankInGroup(Groupid) >= 2 then
			local LowerCaseMessage = string.lower(msg)
			if string.find(LowerCaseMessage, "!rank") then --Look for the rank command to be used
				local SplitMessage = string.split(msg, " ") --Split the message
				local full_player = FindPlr(SplitMessage[2]) --Get the 2nd word in the message
				local playerInstance = game.Players:FindFirstChild(player.Name)
				local req_rank = SplitMessage[3]
				local found = game.Players:FindFirstChild(full_player)
				print(found:GetRankInGroup(Groupid))
				if string.match(req_rank, 'Constable') and found:GetRankInGroup(Groupid) < 6 then
					local offrank = 6
					if full_player == nil then 
						local playerInstance = game.Players:FindFirstChild(player.Name) --Get the player that sent the command.
						game.ReplicatedStorage.Notify:FireClient(playerInstance, "Uh-Oh","We couldn't find the player you wanted to rank!") --Notify the client that it failed.
					else --if player exsists
						local playerInstance = game.Players:FindFirstChild(player.Name) --Find player that sent command.
						game.ReplicatedStorage.Notify:FireClient(playerInstance, "Attempting.","Attempting to rank user...") --Send loading. 
						local Url = "https://"..AppUrl.."/group/rank?username="..full_player.."&key="..ApiKey.."&groupid="..Groupid.."&role_number="..offrank --Add RoleNumber To The Data sent to the WebServer.
						local success, response = pcall(function(data) --Pcall for obvious reasons
							local response = HttpService:GetAsync(Url) --Send request to webserver
							local data = HttpService:JSONDecode(response) --Decode Json Data.
						end)
						if success then --test if worked.
							game.ReplicatedStorage.Notify:FireClient(playerInstance, "Success!", ""..full_player.." has has passed the LR training, and has been ranked as "..req_rank..".") --Custom edited the error messages.
						else
							game.ReplicatedStorage.Notify:FireClient(playerInstance, "UnSuccessful","The command failed") --Custom edited the error messages.
						end
					end
				elseif string.match(req_rank, 'Constable') and found:GetRankInGroup(Groupid) >= 6 then
					game.ReplicatedStorage.Notify:FireClient(playerInstance, "UnSuccessful",""..full_player.." Is already a Constable or higher rank.") --Custom edited the error messages.
				elseif string.match(req_rank, 'Corporal') and found:GetRankInGroup(Groupid) < 7 then
					local offrank = 7
					if full_player == nil then 
						local playerInstance = game.Players:FindFirstChild(player.Name) --Get the player that sent the command.
						game.ReplicatedStorage.Notify:FireClient(playerInstance, "Uh-Oh","We couldn't find the player you wanted to rank!") --Notify the client that it failed.
					else --if player exsists
						local playerInstance = game.Players:FindFirstChild(player.Name) --Find player that sent command.
						game.ReplicatedStorage.Notify:FireClient(playerInstance, "Attempting.","Attempting to rank user...") --Send loading. 
						local Url = "https://"..AppUrl.."/group/rank?username="..full_player.."&key="..ApiKey.."&groupid="..Groupid.."&role_number="..offrank --Add RoleNumber To The Data sent to the WebServer.
						local success, response = pcall(function(data) --Pcall for obvious reasons
							local response = HttpService:GetAsync(Url) --Send request to webserver
							local data = HttpService:JSONDecode(response) --Decode Json Data.
						end)
						if success then --test if worked.
							game.ReplicatedStorage.Notify:FireClient(playerInstance, "Success!", ""..full_player.." has has passed the LR training, and has been ranked as "..req_rank..".") --Custom edited the error messages.
						else
							game.ReplicatedStorage.Notify:FireClient(playerInstance, "UnSuccessful","The command failed") --Custom edited the error messages.
						end
					end
				elseif string.match(req_rank, 'Corporal') and found:GetRankInGroup(Groupid) >= 7 then
					game.ReplicatedStorage.Notify:FireClient(playerInstance, "UnSuccessful",""..full_player.." Is already a Corporal or higher rank.") --Custom edited the error messages.
				elseif string.match(req_rank, 'Sergeant') and found:GetRankInGroup(Groupid) < 8 then
					local offrank = 8
					if full_player == nil then 
						local playerInstance = game.Players:FindFirstChild(player.Name) --Get the player that sent the command.
						game.ReplicatedStorage.Notify:FireClient(playerInstance, "Uh-Oh","We couldn't find the player you wanted to rank!") --Notify the client that it failed.
					else --if player exsists
						local playerInstance = game.Players:FindFirstChild(player.Name) --Find player that sent command.
						game.ReplicatedStorage.Notify:FireClient(playerInstance, "Attempting.","Attempting to rank user...") --Send loading. 
						local Url = "https://"..AppUrl.."/group/rank?username="..full_player.."&key="..ApiKey.."&groupid="..Groupid.."&role_number="..offrank --Add RoleNumber To The Data sent to the WebServer.
						local success, response = pcall(function(data) --Pcall for obvious reasons
							local response = HttpService:GetAsync(Url) --Send request to webserver
							local data = HttpService:JSONDecode(response) --Decode Json Data.
						end)
						if success then --test if worked.
							game.ReplicatedStorage.Notify:FireClient(playerInstance, "Success!", ""..full_player.." has has passed the LR training, and has been ranked as "..req_rank..".") --Custom edited the error messages.
						else
							game.ReplicatedStorage.Notify:FireClient(playerInstance, "UnSuccessful","The command failed") --Custom edited the error messages.
						end
					end
				elseif string.match(req_rank, 'Sergeant') and found:GetRankInGroup(Groupid) >= 8 then
					game.ReplicatedStorage.Notify:FireClient(playerInstance, "UnSuccessful",""..full_player.." Is already a Sergeant or higher rank.") --Custom edited the error messages.
				elseif string.match(req_rank, 'Lieutenant') and found:GetRankInGroup(Groupid) < 9 then
					local offrank = 9
					if full_player == nil then 
						local playerInstance = game.Players:FindFirstChild(player.Name) --Get the player that sent the command.
						game.ReplicatedStorage.Notify:FireClient(playerInstance, "Uh-Oh","We couldn't find the player you wanted to rank!") --Notify the client that it failed.
					else --if player exsists
						local playerInstance = game.Players:FindFirstChild(player.Name) --Find player that sent command.
						game.ReplicatedStorage.Notify:FireClient(playerInstance, "Attempting.","Attempting to rank user...") --Send loading. 
						local Url = "https://"..AppUrl.."/group/rank?username="..full_player.."&key="..ApiKey.."&groupid="..Groupid.."&role_number="..offrank --Add RoleNumber To The Data sent to the WebServer.
						local success, response = pcall(function(data) --Pcall for obvious reasons
							local response = HttpService:GetAsync(Url) --Send request to webserver
							local data = HttpService:JSONDecode(response) --Decode Json Data.
						end)
						if success then --test if worked.
							game.ReplicatedStorage.Notify:FireClient(playerInstance, "Success!", ""..full_player.." has has passed the LR training, and has been ranked as "..req_rank..".") --Custom edited the error messages.
						else
							game.ReplicatedStorage.Notify:FireClient(playerInstance, "UnSuccessful","The command failed") --Custom edited the error messages.
						end
					end
				elseif string.match(req_rank, 'Lieutenant') and found:GetRankInGroup(Groupid) >= 9 then
					game.ReplicatedStorage.Notify:FireClient(playerInstance, "UnSuccessful",""..full_player.." Is already a Lieutenant or higher rank.") --Custom edited the error messages.
				end
			end
		end
	end)
end)

@HayHay You said you were attempting to achieve this, so here you go.

All you have to do is edit the rank numbers and the messages and you’re done.

That is a lot of copy and paste code.

Try using functions and a dict.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.