DarkRP Chat Commands - Ban players from specific list of jobs
16 replies, posted
Hey! I'm new to Lua coding and I'm currently creating a DarkRP server, I want to create a chat command that is: !banjobpd [playername]. I'm very new to Lua and I don't really know how to do this so I was hoping you guys could help me out or point me in the right direction. For example, An Admin types in !banjobpd Danny I then want DarkRP to ban Danny from all PD jobs(listed in the code), So for this the console command executed would be [CODE]darkrp teamban "Danny" "Police Officer"[/CODE] then after that was executed [CODE]darkrp teamban "Danny" "SWAT"[/CODE] and so on. Sorry for going on! I hope you understand what I mean and hopefully I can get support from you guys.
[QUOTE=boxvader;49819241][img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/PlayerSay]GM/PlayerSay[/url]
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/Team]Player:Team[/url]
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.darkrp.com/index.php/Functions/Player/Server/teamBan]teamBan[/url][/QUOTE]
I have this so far(not sure where to go from here) - [CODE]local function BanPlayerFromPD( ply, text, team)
if text == "!banjobpd" then
end
end[/CODE]
It looks to me like you need to learn the basics of glua. You created a random function with values that are non existent. You also failed to use any of the things I provided to you in particular the player say hook.
No one is going to make this for free, if you shows us an actual attempt we will guide you.
I understand, Here is my attempt, will this work?:
[CODE]local function BanPlayerFromPD( ply, text, team)
if string.match(text, "!bjcp") then
str = str:gsub("!bjcp", "")
function() RunConsoleCommand("darkrp teamban ", str, " "Police Officer"")
function() RunConsoleCommand("darkrp teamban ", str, " "SWAT"")
else
end
end
hook.Add( "PlayerSay", BanPlayerFromPD, BanPlayerFromPD )[/CODE]
I'm unable to currently try this.
You are on the right track. First off teamBan is called just like any other function in gmod no need to run console command you can just do ply:teamBan(). Second you need some way of finding the player you want to put the team ban on so you need to take some arguments in your chat command. You will need a name, number for the length and job. So when the person enters the command it should look like this.
[CODE]!bj dan 50 swat[/CODE]
To split up the string text you will want to use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/string/Explode]string.Explode[/url].
Okay, I made this so far, the 999999 is the time which I basically want to be ages and not an option to set with the command. I'm unsure if this is correct.
[CODE]local function BanPlayerFromPD( ply, text, team)
if string.match(text, "!bjcp") then
local str = string.Explode(" ", text)
str[2]:teamBan(TEAM_SWAT, 9999999)
else
end
end
hook.Add( "PlayerSay", BanPlayerFromPD, BanPlayerFromPD )[/CODE]
EDIT: bjcp = Ban Job Cop
Are you looking for this to work only on the police officer job? If so then this is relatively simple and you are pretty much done. If not then there is a lot more you need to implement.
Also you are using team ban incoreclty str[2] is not going to be a player entity it will only be the persons name. You will need to search through all the players on the server by using [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/player/GetAll]player.GetAll[/url] and a in pairs loop.
[CODE] -- Finds the player to perform the ban on
local counter = 0
local plyBan
for k, v in pairs( player.GetAll() ) do
local playerName = string.sub(v:GetName(), 1,char)
if (string.upper(str[2]) == string.upper(playerName)) then
counter = counter + 1
plyBan = v
end
end
if (counter > 1) then ply:PrintMessage( 3, "There were " .. counter .. " players found with the name" .. str[2] .. " ") return false end
if (counter == 0) then ply:PrintMessage( 3, "No players found with the name " .. str[2] .. " ") return false end
[/CODE]
This code will sort through all the players on the server and find the ones that are closest to the name you input. You don't have to type the persons full name for instance if there name was daniel you could type dan and it will find them. However if there is more then 1 person with that in their name it will tell you it will also say if no users where found.
The correct way of formatting team ban is below.
[CODE]plyBan:teamBan(TEAM_CP, 999999)[/CODE]
Hi, thanks for your quick replies, I want it to ban a list of jobs so it would be:
[code]plyBan:teamBan(TEAM_CP, 999999)
plyBan:teamBan(TEAM_SWAT, 999999)
plyBan:teamBan(TEAM_CHIEF, 999999)[/code] All from the same command
[editline]26th February 2016[/editline]
So this is what I've done so far, I also added a unban function:
[CODE]local function BanPlayerFromPD( ply, text, team)
if string.match(text, "!bjcp") then
local str = string.Explode(text)
local counter = 0
local plyBan
for k, v in pairs( player.GetAll() ) do
local playerName = string.sub(v:GetName(), 1,char)
if (string.upper(str[2]) == string.upper(playerName)) then
counter = counter + 1
plyBan = v
v:teamBan(TEAM_SWAT, 999999)
v:teamBan(TEAM_POLICE, 999999)
ply:PrintMessage(v, "was banned from all police jobs")
end
end
if (counter > 1) then ply:PrintMessage( 3, "There were " .. counter .. " players found with the name" .. str[2] .. " ") return false end
if (counter == 0) then ply:PrintMessage( 3, "No players found with the name " .. str[2] .. " ") return false end
if string.match(text, "!ubjcp") then
local str = string.Explode(text)
local counter = 0
local plyBan
for k, v in pairs( player.GetAll() ) do
local playerName = string.sub(v:GetName(), 1,char)
if (string.upper(str[2]) == string.upper(playerName)) then
counter = counter + 1
plyBan = v
v:teamUnBan(TEAM_SWAT)
v:teamUnBan(TEAM_POLICE)
ply:PrintMessage(v, "was unbanned from all police jobs")
end
end
if (counter > 1) then ply:PrintMessage( 3, "There were " .. counter .. " players found with the name" .. str[2] .. " ") return false end
if (counter == 0) then ply:PrintMessage( 3, "No players found with the name " .. str[2] .. " ") return false end
end
end
hook.Add( "PlayerSay", BanPlayerFromPD, BanPlayerFromPD )[/CODE]
EDIT: None of the commands show any sign of working in game.
You put the team ban part in the wrong spot it was supposed to go below the if statements. In addition you where using printmessage incorrectly. I fixed both of those below.
[CODE]
local function BanPlayerFromPD( ply, text, team)
if string.match(text, "!bjcp") then
local str = string.Explode(text)
local counter = 0
local plyBan
for k, v in pairs( player.GetAll() ) do
local playerName = string.sub(v:GetName(), 1,char)
if (string.upper(str[2]) == string.upper(playerName)) then
counter = counter + 1
plyBan = v
end
end
if (counter > 1) then ply:PrintMessage( 3, "There were " .. counter .. " players found with the name" .. str[2] .. " ") return false end
if (counter == 0) then ply:PrintMessage( 3, "No players found with the name " .. str[2] .. " ") return false end
-- TEAM BAN GOES DOWN HERE
plyBan:teamBan(TEAM_SWAT)
plyBan:teamBan(TEAM_POLICE)
ply:PrintMessage(3, plyBan .. "was banned from all police jobs")
if string.match(text, "!ubjcp") then
local str = string.Explode(text)
local counter = 0
local plyBan
for k, v in pairs( player.GetAll() ) do
local playerName = string.sub(v:GetName(), 1,char)
if (string.upper(str[2]) == string.upper(playerName)) then
counter = counter + 1
plyBan = v
end
end
if (counter > 1) then ply:PrintMessage( 3, "There were " .. counter .. " players found with the name" .. str[2] .. " ") return false end
if (counter == 0) then ply:PrintMessage( 3, "No players found with the name " .. str[2] .. " ") return false end
end
-- TEAM UNBAN GOES DOWN HERE
plyBan:teamUnBan(TEAM_SWAT)
plyBan:teamUnBan(TEAM_POLICE)
ply:PrintMessage(3, plyBan .. "was unbanned from all police jobs")
end
hook.Add( "PlayerSay", BanPlayerFromPD, BanPlayerFromPD )
[/CODE]
Hi, Thank you very much for your help it is much appreciated. I made the changes and nothing is actually happening in game still. Do I have it in the correct directory?: lua\autorun\server\commands.lua
Your hook is wrong should look like this
[CODE]hook.Add( "PlayerSay", "BanPlayerFromPD", BanPlayerFromPD )[/CODE]
Ah Thanks for spotting that, For some reason the command is still not showing any sign of working, I don't see what the problem is. I'm entering this into chat: !bjcp danny
[QUOTE=Danny F;49820584]Ah Thanks for spotting that, For some reason the command is still not showing any sign of working, I don't see what the problem is. I'm entering this into chat: !bjcp danny[/QUOTE]
It's because you are using string.match against the entire entered text when it should only be on the first part of the command since you added an argument.
Try this instead
[CODE]
local function BanPlayerFromPD( ply, text, team)
local str = string.Explode(text)
if string.match(str[1], "!bjcp") then
local counter = 0
local plyBan
for k, v in pairs( player.GetAll() ) do
local playerName = string.sub(v:GetName(), 1,char)
if (string.upper(str[2]) == string.upper(playerName)) then
counter = counter + 1
plyBan = v
end
end
if (counter > 1) then ply:PrintMessage( 3, "There were " .. counter .. " players found with the name" .. str[2] .. " ") return false end
if (counter == 0) then ply:PrintMessage( 3, "No players found with the name " .. str[2] .. " ") return false end
-- TEAM BAN GOES DOWN HERE
plyBan:teamBan(TEAM_SWAT)
plyBan:teamBan(TEAM_POLICE)
ply:PrintMessage(3, plyBan .. "was banned from all police jobs")
local str = string.Explode(text)
if string.match(str[1], "!ubjcp") then
local counter = 0
local plyBan
for k, v in pairs( player.GetAll() ) do
local playerName = string.sub(v:GetName(), 1,char)
if (string.upper(str[2]) == string.upper(playerName)) then
counter = counter + 1
plyBan = v
end
end
if (counter > 1) then ply:PrintMessage( 3, "There were " .. counter .. " players found with the name" .. str[2] .. " ") return false end
if (counter == 0) then ply:PrintMessage( 3, "No players found with the name " .. str[2] .. " ") return false end
end
-- TEAM UNBAN GOES DOWN HERE
plyBan:teamUnBan(TEAM_SWAT)
plyBan:teamUnBan(TEAM_POLICE)
ply:PrintMessage(3, plyBan .. "was unbanned from all police jobs")
end
hook.Add( "PlayerSay", BanPlayerFromPD, BanPlayerFromPD )
[/CODE]
Sadly still no luck :(
I also just noticed you are using team ban incorrectly you don't have a time listed.
If things still aren't working then throw a print statement into the code to see if it is running.
This is probably the last post I will make on this as I have done a lot of work for you and it's time you put some effort in before coming back. I caught two simple errors that you could have found if you looked back over the code so make sure you look over it thoroughly.
I do have a custom ULX command I made a while back:
[CODE]
local CATEGORY_NAME = "Tupac's Commands"
function ulx.demote( calling_ply, target_ply )
if target_ply:Team() == GAMEMODE.DefaultTeam then
ULib.tsayError( calling_ply, target_ply:Nick() .. " is already Citizen!", true )
else
target_ply:teamBan()
target_ply:changeTeam(GAMEMODE.DefaultTeam, true)
ulx.fancyLogAdmin( calling_ply, true, "#A demoted #T", target_ply )
end
end
local demote = ulx.command ( CATEGORY_NAME, "ulx demote", ulx.demote, "!demote", false )
demote:addParam{ type=ULib.cmds.PlayerArg }
demote:defaultAccess( ULib.ACCESS_ADMIN )
demote:help( "Demotes someone to Citizen" )
[/CODE]
Sorry, you need to Log In to post a reply to this thread.