[HELP] Restricting a command for admins

Hello, i was trying to restrict “/car” command for admins, i tried several times but it’s not working.

Here is the script:

TriggerEvent('es:addCommand', 'car', "admin", function(source, args, user)
		if(GetPlayerName(tonumber(args[2])))then
			local player = tonumber(args[2])
			-- User permission check
			TriggerEvent("es:getPlayerFromId", source, function(user)
				if user.permission_level == (2) then
				TriggerClientEvent('es_admin:spawnVehicle', source, args[2]) --SPAWN CARS
				TriggerClientEvent("chatMessage", source, "SYSTEM", {255, 0, 0}, "You spawned a car!")
			else
				TriggerClientEvent("chatMessage", source, "SYSTEM", {255, 0, 0}, "You are not allowerd to use this command!")
		end
	end)
end)

May someone help me fix this? Thanks in advance!

1 Like

Fist of all, what errors do you get? What do you expect to happen and what actually happens?

Also, why do you do a “permission check” when you’re making the command only executable by the “admins” group?

I honestly can’t say more than that because of the horrid indentation you’re using (I’m pretty sure you’re missing an end somewhere).

And on another note, try adding another function to the “es:addCommand” event handler for if the player doesn’t have permission (I also think it should be something like addGroupCommand, have a look at the documentation).

As @Havoc pointed out, you’re indeed missing an end. First of all fix that, then look for errors.

TriggerEvent('es:addCommand', 'car', "admin", function(source, args, user)
	if(GetPlayerName(tonumber(args[2]))) then
		local player = tonumber(args[2])
		-- User permission check
		TriggerEvent("es:getPlayerFromId", source, function(user)
			if user.permission_level == (2) then
				TriggerClientEvent('es_admin:spawnVehicle', source, args[2]) --SPAWN CARS
				TriggerClientEvent("chatMessage", source, "SYSTEM", {255, 0, 0}, "You spawned a car!")
			else
				TriggerClientEvent("chatMessage", source, "SYSTEM", {255, 0, 0}, "You are not allowerd to use this command!")
			end
		end)
	end
end)
1 Like

I tried this:

TriggerEvent('es:addCommand', 'car', "admin", function(source, args, user)
		if(GetPlayerName(tonumber(args[2])))then
			local player = tonumber(args[2])
			-- User permission check
			if user.permission_level == (2) then
			TriggerClientEvent('es_admin:spawnVehicle', source, args[2]) --SPAWN CARS
			TriggerClientEvent("chatMessage", source, "SERVER", {255, 0, 0}, "You spawned a car!")
			end
		else
			if user.permission_level == (0) then
			TriggerClientEvent("chatMessage", source, "ERROR", {255, 0, 0}, "You are not allowerd to use this command!")
			end
	end)

and i get this error:

Unexpected token “)” , “end” expected

But i don’t get where it is missing

I missed one “end” and now i don’t get the error. I will test it in game to see if it works!

I do not get the error but of course it is not working. Nothing happens when i type the command in chat!

I don’t know how “es” works, but add some means of debugging the code - see where it fails. Since it’s a server-side script you can either use RconPrint("") if you have access to the RCon window or the “chatMessage” event.

I kinda fixed it like this:

TriggerEvent('es:addCommand', 'car', function(source, args, user)
		if(GetPlayerName(tonumber(args[2])))then
			local player = tonumber(args[2])

			if user.permission_level == (2) then
			TriggerClientEvent('es_admin:spawnVehicle', source, args[2]) --SPAWN CARS
			TriggerClientEvent("chatMessage", source, "SERVER", {255, 0, 0}, "You spawned a car!")
			end
		else
			if user.permission_level == (0) then
			TriggerClientEvent("chatMessage", source, "ERROR", {255, 0, 0}, "You are not allowerd to use this command!")
			end
		end
	end)

I get the message that i cant use the command if i’m permission level 0, but i get nothing if i am level 2 permission. Any solutions?

I’d suggest you take a look at how if statements work in Lua and for your own as well as others inconvenience - how to properly tabulate (indentation) your code. It makes it a lot easier to read and spot mistakes.

TriggerEvent('es:addCommand', 'car', function(source, args, user)
	if(GetPlayerName(tonumber(args[2]))) then
		local player = tonumber(args[2])

		if(user.permission_level == 2) then
			TriggerClientEvent('es_admin:spawnVehicle', source, args[2]) --SPAWN CARS
			TriggerClientEvent("chatMessage", source, "SERVER", {255, 0, 0}, "You spawned a car!")
		elseif(user.permission_level == 0) then
			TriggerClientEvent("chatMessage", source, "ERROR", {255, 0, 0}, "You are not allowerd to use this command!")
		end
	end
end)

Still not working, the usage of the command its like /car (playerid) (vehname) but this not working i just get the message (You spawned a vehicle) or (You cannot use this command) and nothing happens. Also do you know a guide to train indentation? Thank you.

You’re getting the error message because you’re not at permission level 2.

First of all, try using the es:addGroupCommand event instead, it should only allow admins to use the command meaning you don’t need to check the permission level yourself.

You should also check that args[1] and args[2] exist before sending them to events or doing whatever with them.

--[[
args 1 = player name
Args 2 = vehicle
]]
if args[1] and args[2] then
   -- do command
else
  -- don't do command
end
1 Like

Thank you very much! Helped me a lot!

1 Like

How can I allow mods to revive too?

i am having trouble trying to restrict this type of command if someone can help. i want to make it only for group.leo

if sm[1] == “/hospital” then