Admin commands is not working

Hello, I am trying to make admin commands using the chat but it doesn’t work.

Script:

local Chat = game:GetService("Chat")
local Players = game:GetService("Players")
local IsPrivateServer = false
local PrivateServerOwner
local AdminList = {}

local Settings = {
	Prefix = ":";
}

local function LogMessage(message, logtype)
	if logtype == "print" then
		game.ReplicatedStorage.Events.LogMessage:FireAllClients(message, logtype)
	elseif logtype == "warn" then
		game.ReplicatedStorage.Events.LogMessage:FireAllClients(message, logtype)
	end
end

local function getServerType()
	if game.PrivateServerId ~= "" then
		if game.PrivateServerOwnerId ~= 0 then
			return "PrivateServer"
		else
			return "ReservedServer"
		end
	else
		return "StandardServer"
	end
end

local function GetPlayer(target)
	for i,v in pairs(game.Players:GetPlayers()) do 
		if v.Name:lower():sub(1,#target) == target:lower() then
			return v
		end
	end
end

local ServerType = getServerType()
if ServerType == "PrivateServer" then
	IsPrivateServer = true
	PrivateServerOwner = game.PrivateServerOwnerId
	table.insert(AdminList, PrivateServerOwner)
end

Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if IsPrivateServer then
		local Character = player.Character
		local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
		local command = message:lower()
			if command:sub(1, 6) == Settings.Prefix .. "admin" and player.UserId == PrivateServerOwner then
			local commandplayer = GetPlayer(command:sub(8))
				if commandplayer ~= nil then
					if table.find(AdminList, commandplayer.UserId) == nil then
					table.insert(AdminList, commandplayer.UserId)
						
					elseif command:sub(1, 8) == Settings.Prefix .. "unadmin" and player.UserId == PrivateServerOwner then
							local commandplayer = GetPlayer(command:sub(10))
							if commandplayer ~= nil then
							if table.find(AdminList, commandplayer.UserId) ~= nil then
								table.remove(AdminList, commandplayer.UserId)
								
							elseif command:sub(1, 3) == Settings.Prefix .. "to" and table.find(AdminList, player.UserId) ~= nil then
								local commandplayer = GetPlayer(command:sub(5))
								if commandplayer ~= nil then
								local commandcharacter = commandplayer.Character
								local HumanoidRootPartcommand = commandcharacter:FindFirstChild("HumanoidRootPart")
									if HumanoidRootPart and HumanoidRootPartcommand then
									local offset = 5
										HumanoidRootPart.CFrame = HumanoidRootPartcommand.CFrame + HumanoidRootPartcommand.CFrame.lookVector * offset
										
									elseif command:sub(1, 6) == Settings.Prefix .. "bring" and table.find(AdminList, player.UserId) ~= nil then
										local commandplayer = GetPlayer(command:sub(8))
										if commandplayer ~= nil then
											local commandcharacter = commandplayer.Character
											local HumanoidRootPartcommand = commandcharacter:FindFirstChild("HumanoidRootPart")
											if HumanoidRootPart and HumanoidRootPartcommand then
												local offset = 5
												HumanoidRootPartcommand.CFrame = HumanoidRootPart.CFrame + HumanoidRootPart.CFrame.lookVector * offset
										end
									end
								end
							end
					      end
				       end
					end
				end
			end
		end
	end)
end)
1 Like

When you say it isn’t working, could you elaborate? Is it just in private servers or in general it isn’t working?

Hello, the admin commands is only for private servers. I was testing it on a private server.

Oh my apologies, have you tried adding print statements to see where the script is breaking?

Please don’t use if statements to determine what command to use. With many commands things will be very inefficient…

Use a module script. Using the first argument of a message (i.e, kick) you can get a function to fire on a module script. Send some arguments to it as well, as a table of separated strings based on spaces.

--module script
local module = {}

function module.kick(player,arguments)
arguments[2]:Kick()
end

function module.kill(player,arguments)
arguments[2].Character:BreakJoints()
end

return module 

--in the chat script use the first argument of the message to find a function in the module script and fire it

I don’t think the roblox studio plates detects private servers or anything. I’m not really sure what’s going on in the code, does it return errors or anything?

2 Likes

The only command that worked is :admin (player) and the others do not work

Have you tried adding in print statements in the broken commands themselves?

Also you’ve mentioned the :admin command works, does the :unadmin command also work?

No, the command :unadmin does not work. And I added print statements and nothing is printed on the console.

Apologies if it was just the Roblox Lua emulator messing up indentation, but when I paste the code into a script, the other command if statements are inside of the :admin if statement, so the elseif statements would only run if the player types :admin and then also types :unadmin which is impossible (or whatever the command is), does this have anything to do with it?