How to add rank command ontop of the existing tutorial?

Ok so I figured out why it was saying “Demote” instead of rank and I fixed that but it still does nothing.


I changed the bit circled from demote to rank in case it was calling the wrong AppURL but nothing happened with either. I get why it says demote instead of rank and have fixed that but do you have any ideas what could be going wrong with the functionality?

Before line

“If success”, do print(data)

It says “Unknown global “Data””. Also when testing the command will it work in Studio or shall I connect to the public place and try ranking my alt? But yes it says that data is unknown.

Okay, put the print(data) inside of the pcall.

Then see what it prints.

Ok! Got this:

Much much better, alright, so what I think the issue is, is that’s its not getting the 3rd word of the sentence.

There must be an error within my code, let me try fix it, when you run in the command can you show me what you run?

If that ever happens just tell me on dizzy or something and I can sort it out but yeh, don’t worry for now.

For testing it works for me, can you just tell me the command you use in chat?

!rank Kerm 4

(Kerm is my name but it didn’t work for my alt either and 4 is the rank in the group as yno!)

Alright, I may have another idea.

It gets late so my coding degrades, I think I finally have the idea now!

1 Like

Alright, where it says local req_rank = findPlr(SplitMessage[3]) change it to local req_rank = SplitMessage[3].

Tell me if it works!

Oh my god it worked! You are an absolute legend THANK YOU!

This should be the final script:

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, "!promote") then
				local SplitMessage = string.split(msg, " ")
				local full_player = FindPlr(SplitMessage[2])
				if full_player == nil then
					local playerInstance = game.Players:FindFirstChild(player.Name)
					game.ReplicatedStorage.Notify:FireClient(playerInstance, "Uh-Oh","We couldn't find the player you wanted to promote!")
				else
					local playerInstance = game.Players:FindFirstChild(player.Name)
					game.ReplicatedStorage.Notify:FireClient(playerInstance, "Attempting.","Attempting to promote user...")
					local Url = script.AppUrl.Value.."group/promote?user_name="..full_player.."&key=" ..script.ApiKey.Value.."&groupid="..script.GroupId.Value
					local success, response = pcall(function()
						local response = HttpService:GetAsync(Url)
						local data = HttpService:JSONDecode(response)
					end)
					if success then
						game.ReplicatedStorage.Notify:FireClient(playerInstance, "Horrah!","We promoteed the user!")
					else
						game.ReplicatedStorage.Notify:FireClient(playerInstance, "Uh-Oh","We had problems promoting the user.")
					end
				end
			else if string.find(LowerCaseMessage, "!demote") then
					local SplitMessage = string.split(msg, " ")
					local full_player = FindPlr(SplitMessage[2])
					if full_player == nil then
						local playerInstance = game.Players:FindFirstChild(player.Name)
						game.ReplicatedStorage.Notify:FireClient(playerInstance, "Uh-Oh","We couldn't find the player you wanted to demote!")
					else
						local playerInstance = game.Players:FindFirstChild(player.Name)
						game.ReplicatedStorage.Notify:FireClient(playerInstance, "Attempting.","Attempting to demote user...")
						local Url = script.AppUrl.Value.."group/demote?user_name="..full_player.. "&key=" ..script.ApiKey.Value.. "&groupid=" ..script.GroupId.Value
						local success, response = pcall(function()
							local response = HttpService:GetAsync(Url)
							local data = HttpService:JSONDecode(response)
						end)
						if success then
							game.ReplicatedStorage.Notify:FireClient(playerInstance, "Horrah!","We demoted the user!")
						else
							game.ReplicatedStorage.Notify:FireClient(playerInstance, "Uh-Oh","We had problems demoting. the user.")
						end
					end
				else 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 req_rank = SplitMessage[3]

						--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 = script.AppUrl.Value.."group/rank?user_name="..full_player.. "&key=" ..script.ApiKey.Value.. "&groupid=" ..script.GroupId.Value.. "&role_number=" ..req_rank --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, "Horrah!","We changed the rank of the user the user!") --Custom edited the error messages.
							else
								game.ReplicatedStorage.Notify:FireClient(playerInstance, "Uh-Oh","We had problems changing the rank of the user.") --Custom edited the error messages.
							end
						end
					end
				end
			end
		end
	end)
end)

Please mark this post as solution, also please double check I haven’t made a typo .

2 Likes

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