Help with Random Weapon Script

I’m creating a script for my ttt server for learning purposes that will give a random primary and secondary weapon to each player when they spawn. This is actually a two part question.

My first question is how do you guys test you’re code? I’m trying to test portions in single player then update my server file with the working code. Is there a better way to do this?

Second what did I do wrong with the array in this code that immediately gives me lua errors upon starting a new sp game?


function testfile (ply)
    weapons = {
        primary = {
            { name = "weapon_zm_shotgun", ammo = "item_box_buckshot_ttt" }
            { name = "weapon_ttt_m16", ammo = "item_ammo_pistol_ttt"}
            { name = "weapon_zm_mac10", ammo = "item_ammo_smg1_ttt"}
            { name = "weapon_zm_rifle", ammo = "item_ammo_357_ttt"}
            { name = "weapon_zm_sledge", ammo = ""}
        }
        secondary = {
            { name = "weapon_zm_revolver", ammo = "item_ammo_revolver_ttt"}
            { name = "weapon_zm_pistol", ammo = "item_ammo_pistol_ttt"}
            { name = "weapon_ttt_glock", ammo = "item_ammo_pistol_ttt"}
        }
    }
    primaryweapon = math.random(1,5)
    secondaryweapon = math.random(1,3)
    
    Msg("Primary: " .. weapons.primary[primaryweapon].name)
end
concommand.Add("testfile", testfile)

I have this code in my lua/autorun/client folder if that helps.

[editline]14th May 2013[/editline]

Thanks to this post I was able to fix my table problem.

Working code:


function testfile (ply)
    weapons = {
        {["type"]="primary", ["name"]="weapon_zm_shotgun", ["ammo"]="item_box_buckshot_ttt"},
        {["type"]="primary", ["name"]="weapon_ttt_m16", ["ammo"]="item_ammo_pistol_ttt"},
        {["type"]="primary", ["name"]="weapon_zm_mac10", ["ammo"]="item_ammo_smg1_ttt"},
        {["type"]="primary", ["name"]="weapon_zm_rifle", ["ammo"]="item_ammo_357_ttt"},
        {["type"]="primary", ["name"]="weapon_zm_sledge", ["ammo"]=""},
        
        {["type"]="secondary", ["name"]="weapon_zm_revolver", ["ammo"]="item_ammo_revolver_ttt"},
        {["type"]="secondary", ["name"]="weapon_zm_pistol", ["ammo"]="item_ammo_pistol_ttt"},
        {["type"]="secondary", ["name"]="weapon_ttt_glock", ["ammo"]="item_ammo_revolver_ttt"}
        
        
    }

    primaryweapon = math.random(1,5)
    secondaryweapon = math.random(6,8)

    Msg("Primary: " .. weapons[primaryweapon].name .. "
")
    Msg("Secondary: " .. weapons[secondaryweapon].name .. "
")
end
concommand.Add("testfile", testfile)

However I’d still like to hear ya’lls coding techniques. I’m going to move on to giving players the weapons on spawn. Wish me luck.

Thanks.

[editline]14th May 2013[/editline]

Also here’s my server file I’m hoping it doesn’t crash when I upload it in a bit.


if SERVER then
    AddCSLuaFile("randomweaponloadout.lua")
end

local weapons = {
        {["type"]="primary", ["name"]="weapon_zm_shotgun", ["ammo"]="item_box_buckshot_ttt"},
        {["type"]="primary", ["name"]="weapon_ttt_m16", ["ammo"]="item_ammo_pistol_ttt"},
        {["type"]="primary", ["name"]="weapon_zm_mac10", ["ammo"]="item_ammo_smg1_ttt"},
        {["type"]="primary", ["name"]="weapon_zm_rifle", ["ammo"]="item_ammo_357_ttt"},
        {["type"]="primary", ["name"]="weapon_zm_sledge", ["ammo"]=""},
        
        {["type"]="secondary", ["name"]="weapon_zm_revolver", ["ammo"]="item_ammo_revolver_ttt"},
        {["type"]="secondary", ["name"]="weapon_zm_pistol", ["ammo"]="item_ammo_pistol_ttt"},
        {["type"]="secondary", ["name"]="weapon_ttt_glock", ["ammo"]="item_ammo_revolver_ttt"}
        
        
    }

hook.Add("PlayerLoadout", "GiveWeapon", function(ply)
    local primaryweapon = math.random(1,5)
    local secondaryweapon = math.random(6,8)
    
    ply:Give(weapons[primaryweapon].name)
    ply:Give(weapons[primaryweapon].ammo)
    ply:Give(weapons[secondaryweapon].name)
    ply:Give(weapons[secondaryweapon].ammo)
end)

Couple of things the Huge is still buggy so you have to make sure not to attempt to add ammo to it. It also can make you’re model bug out and no one can move you’re body once you die or examine it.

I added a few things like grenades.


if SERVER then
    AddCSLuaFile("randomweaponloadout.lua")
end

local weapons = {
    {["type"]="primary", ["name"]="weapon_zm_shotgun", ["ammo"]="Buckshot", ["clipsize"]=24},
    {["type"]="primary", ["name"]="weapon_ttt_m16", ["ammo"]="Pistol", ["clipsize"]=60},
    {["type"]="primary", ["name"]="weapon_zm_mac10", ["ammo"]="SMG1", ["clipsize"]=60},
    {["type"]="primary", ["name"]="weapon_zm_rifle", ["ammo"]="357", ["clipsize"]=20},
    {["type"]="primary", ["name"]="weapon_zm_sledge", ["ammo"]="", ["clipsize"]=""},
    
    {["type"]="secondary", ["name"]="weapon_zm_revolver", ["ammo"]="AlyxGun", ["clipsize"]=36},
    {["type"]="secondary", ["name"]="weapon_zm_pistol", ["ammo"]="Pistol", ["clipsize"]=60},
    {["type"]="secondary", ["name"]="weapon_ttt_glock", ["ammo"]="Pistol", ["clipsize"]=60},
    
    {["type"]="grenade", ["name"]="weapon_tttbasegrenade"},
    {["type"]="grenade", ["name"]="weapon_zm_molotov"},
    {["type"]="grenade", ["name"]="weapon_ttt_smokegrenade"}
}

hook.Add("PlayerLoadout", "GiveWeapon", function(ply)
    // select random weapons
    local primaryweapon = math.random(1,5)
    local secondaryweapon = math.random(6,8)
    local grenade = math.random(9,11)
    
    // primary
    ply:Give(weapons[primaryweapon].name)
    
    // secondary
    ply:Give(weapons[secondaryweapon].name)
    
    // ammo
    if weapons[primaryweapon].ammo == weapons[secondaryweapon].ammo then
        ply:GiveAmmo(weapons[primaryweapon].clipsize, weapons[primaryweapon].ammo)
    elseif weapons[primaryweapon].ammo == "" then
        ply:GiveAmmo(weapons[secondaryweapon].clipsize, weapons[secondaryweapon].ammo)
    else
        ply:GiveAmmo(weapons[primaryweapon].clipsize, weapons[primaryweapon].ammo)
        ply:GiveAmmo(weapons[secondaryweapon].clipsize, weapons[secondaryweapon].ammo)
    end
    
    // grenade
    ply:Give(weapons[grenade].name)
end)

Working code.

That doesn’t take into account future changes to the table

Revised code


if SERVER then
    AddCSLuaFile("randomweaponloadout.lua")
end

local weapons = {
    primary= {
        {["name"]="weapon_zm_shotgun", ["ammo"]="Buckshot", ["clipsize"]=24},
        {["name"]="weapon_ttt_m16", ["ammo"]="Pistol", ["clipsize"]=60},
        {["name"]="weapon_zm_mac10", ["ammo"]="SMG1", ["clipsize"]=60},
        {["name"]="weapon_zm_rifle", ["ammo"]="357", ["clipsize"]=20},
        {["name"]="weapon_zm_sledge", ["ammo"]="", ["clipsize"]=""}
    },
    
    secondary = {
        {["name"]="weapon_zm_revolver", ["ammo"]="AlyxGun", ["clipsize"]=36},
        {["name"]="weapon_zm_pistol", ["ammo"]="Pistol", ["clipsize"]=60},
        {["name"]="weapon_ttt_glock", ["ammo"]="Pistol", ["clipsize"]=60}
    },
    
    grenade = {
        {["name"]="weapon_tttbasegrenade"},
        {["name"]="weapon_zm_molotov"},
        {["name"]="weapon_ttt_smokegrenade"}
    }
}

hook.Add("PlayerLoadout", "GiveWeapon", function(ply)
    // select random weapons
    local primaryweapon = table.Random( weapons.primary )
    local secondaryweapon = table.Random( weapons.secondary )
    local grenade = table.Random( weapons.grenade )
    
    // primary
    ply:Give(primaryweapon.name)
    
    // secondary
    ply:Give(secondaryweapon.name)
    
    // ammo
    if primaryweapon.ammo == secondaryweapon.ammo then
        ply:GiveAmmo(primaryweapon.clipsize, primaryweapon.ammo)
    elseif primaryweapon.ammo == "" then
        ply:GiveAmmo(secondaryweapon.clipsize, secondaryweapon.ammo)
    else
        ply:GiveAmmo(primaryweapon.clipsize, primaryweapon.ammo)
        ply:GiveAmmo(secondaryweapon.clipsize, secondaryweapon.ammo)
    end
    
    // grenade
    ply:Give(grenade.name)
end)

Thanks a lot I was going to look into that eventually!