Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Admin script still prints the error message for everyone if they don't use the prefix?

Okay, so I am making an admin script and have run into a problem;

It prints the error message if a player tried to use a command without even using the prefix. And even does it if they are admin. But it does still do it correct if they use the prefix.

Here is my code;

local Admins = {["mosski123"] = true, ["Player1"] = true}
local prefix = ';' 

function onChatted(message, player)
    if message == prefix.."reset" and Admins[player.Name] then
        player.Character:BreakJoints()
else
	print("[LINC] "..player.Name.. " tried to use the 'reset command!'")
end
	if message == prefix.."rejoin" and Admins[player.Name] then
		game:GetService("TeleportService"):Teleport(game.PlaceId, player)
		if player.Character ~= nil then
		player.Character:remove()
		end
	end
	
end
 
game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(message) onChatted(message, player) end)
end)

1 answer

✓ Accepted answer

There is a small syntax error:-

-- you missed the  .. after player.Name
print("[LINC] "..player.Name " tried to use the 'reset command!'")

The second possible problem is the way you have setup the logic so:-

local Admins = {["mosski123"] = true, ["Player1"] = true}
local prefix = ';' 

function onChatted(message, player)
	-- we run this if statment
	if message == prefix.."reset" and Admins[player.Name] then
		-- code
	else
	   -- code
	end
	
	-- we also run this statment even if we have ran a command
	if message == prefix.."rejoin" and Admins[player.Name] then
		-- code
	end
end

-- we should use elseif as only one of the ifs will run as only one command will do one thing
if [condition] then
	-- code
elseif [second condition] then
	-- code
end

There are some other things that should be changes in your code, firstly I would recommend that you use UserId instead of the players name as the id will not change. Secondly you do a lot of processing that can be done once ie, prefix.."reset" and Admins[player.Name].

Example:-

-- we should use the player id not their name
local Admins = {["1540993"] = true, ["0"] = true} -- my id ect
local prefix = ';' 

-- just an idea to remove the long if statements, you can still use your method but you would need to use elseif
local commandTable = {}

function commandTable.rejoin(message, player)
	player.Character:BreakJoints()
end


game.Players.PlayerAdded:connect(function(player)
	if Admins[tostring(player.UserId)] then -- we only connect the admin command chatted event for admins instead of checking this each time
		player.Chatted:connect(function(message) 
			if message:sub(1,1) == prefix then -- we check for a prefix before checking for commands
				
				-- we remove the prefix its not needed
				message = message:sub(2, #message)

				if commandTable[message] then -- we have a valid command
					commandTable[message](message, player) -- we run the function in the table passing it the same data as in your code.
				end
			end
		end)
	end
end)

I hope this helps, please comment if you do not understand how / why this code works.

3 comments

0

I will implement this into my game now, and if it works, I'll upvote and accept, if not I'll let you know.

0

I'm sorta confused on how to implement it.

0

Which part?

This discussion is preserved as it was. New answers, comments, and votes are closed.