Search code examples
luagarrys-mod

(Garry's Mod DarkRP) Wildcard selector from ammo.lua searching jobs.lua


I'm not sure if this is the right section for games, but still. I am a developer for a in-dev server that I believe uses FAdmin & ULX for the admin.. things.. anyway; I am currently developing the jobs.lua, shipments.lua, and ammo.lua. It's supposed to be a militaryrp, so there's obviously alot of custom-made teams. Shipments? Easy. Jobs? Easy. Ammo? Well, it might be easy if this question has the solution I'm looking for. Here's an excerpt for 2 jobs, 2 shipments, and 1 ammo type. I'll explain for each section.

TEAM_ARMY = DarkRP.createJob("Army Infantry", {
 color = Color(0, 0, 255, 0),
 model = "models/codmw2/codmw2h.mdl",    
 description = [[You are an Army Infantry! 
Please read the rules found in the !motd, it describes the limitations of each job very well!]],    
 weapons = {"fas2_g3", "fas2_glock20", "fas2_dv2"},
 command = "armyi",    
 max = 5,   
 salary = 80,
 admin = 0,
 vote = false,
 hasLicense = false
})

TEAM_ARMYSH = DarkRP.createJob("Army Shotgun", {
 color = Color(0, 0, 255, 0),
 model = "models/codmw2/codmw2.mdl",    
 description = [[You are an Army Support! 
Please read the rules found in the !motd, it describes the limitations of each job very well!]],    
 weapons = {"fas2_m3s90", "fas2_ots33", "fas2_dv2", "riot_shield"},
 command = "armys",    
 max = 4,   
 salary = 100,
 admin = 0,
 vote = false,
 hasLicense = false
})

Here are two custom ARMY jobs [teams]. This is the problem I'm facing. I need to be able to [let's say] select these two plus countless other jobs for the ammo type below that targets them to be the only teams/jobs that can buy that ammo type. I've seen the tutorial on the DarkRP Wiki, but that's not the correct format for this sever. The ammo types are being imported as FAS2 ammo.

DarkRP.createAmmoType("5.56x45MM",  {
 name = "5.56x45MM",
 model = "models/items/boxsrounds.mdl",
 price = 145,
 amountGiven = 60,
 customCheck = function(ply) return CLIENT or ply:IsTeam("TEAM_ARMY") or ply:IsTeam(TEAM_%ARMY$) or ply:IsUserGroup("owner") end,
  CustomCheckFailMsg = "You must be apart of the Army!"
})

What I attempted to do is this; If the player is on the TEAM_ARMY, or the Army Infantry job, OR is on the TEAM_ARMY%, where % is supposed to be a wildcard, then that means it can target lets say TEAM_ARMYSH for Army Shotgun and also TEAM_ARMYS for Army Sniper, if those were the only two other jobs that had ARMY beginning with it [although that's NOT the case.]

The shipments makes it easier; there's a simple allowed = {} cmd line, which can be used like this.

AddCustomShipment("AK12", "models/weapons/world/rifles/w_ak12.mdl",  "fas2_ak12",  3500,  1  true,  3500,  true,  {TEAM_TALI})

AddCustomShipment("AK47",  "models/weapons/w_ak47.mdl",  "fas2_ak47",  4000,  1  true,  4000,  true,  {TEAM_TALI, TEAM_BMARKET})

The attributes go [in order]: Name; Model; Entity; Price; Amount; Seperate; PriceSep; NoShip; and Allowed. The allowed simply lets me target a team for the usage. Sadly, ammo does not. So what I'm trying to figure out is how can I select multiple groups from one ply:IsTeam("TEAM_ARMY") that begin with 'ARMY' and have some sort of wildcard after ARMY for selection? This is so that I don't flood the ammo types with tons and tons of groups.


Solution

  • You can do something like:

     customCheck = function(ply) return string.find(string.lower(team.GetName( ply:Team() )),"army") or ply:IsUserGroup("owner") end, 
    

    string.find(<haystack>,<needle>) will return a number if it finds <needle> in <haystack>:

    Example:

    string.find("ABC","B") returns 2 since it's the second letter.

    Now string.lower(<text>) returns a lowercase version of <text>

    Example:

    string.lower("Hello World!") returns "hello world!"

    Now team.GetName(<number>) gets the team name biased on the team number and to get the number I used ply:Team() which returns a number.

    Hopefully this will help you and you can get back to working on it quickly.

    If it doesn't work just comment, this code is untested.