Skip to content

Adding Custom Weapon

Wolvindra-Vinzuerio edited this page Jan 18, 2024 · 3 revisions

This is updated version of previously discussed on PH:X Community thread but currently inaccessible. If you came from this guide this is the enhanced version of it.

Adding Custom Weapons

Adding custom weapons is fairly easy for hunters.

Instead of having to modify in game, I highly suggest to use this "external" script to avoid game from breaking.

⚠ Currently it's impossible to add weapons for Props but I highly suggest to use LPS weapon addons instead: https://github.com/Wolvin-NET/prophuntx-lps-weapons (also example scripts are provided here)

New Method #1: Using Hunter Loadout Editor

Use PH:X2Z Plugin to modify Hunter's Loadout: https://steamcommunity.com/sharedfiles/filedetails/?id=3141486825

New Method #2: (Using Hook) - Only on **Revision 09.01.2024+**

  1. Go to your Garry's Mod Main server folder and navigate to garrysmod/lua/autorun/server folder. (Multiplayer/LAN Game is the same thing)
  2. Create a new file called ph_custom_weapons.lua inside the garrysmod/lua/autorun/server folder.
  3. You can paste these code and use which weapon spawning method do you like. You can toggle config SpawnRandomLoadout = true <or> false and KeepDefaultWeapon = true <or> false in the code.
local SpawnRandomLoadout = false --true to use Random Loadouts
local KeepDefaultWeapon = false --true to keep default Prop Hunt weapon (Crowbar, 357, and Shotgun)

--//////// Simple Loadout Weapons Table ////////--
local Simple_Loadout = {
	"tfa_csgo_ak47",
	"cw2_mp41",
	"weapon_ak47",
	"tfa_your_weapon_names_here",
	"weapon names here",
	-- "add more here",
	-- "add more here",
}

--//////// Random Loadout Weapons Table ////////--
local Random_Loadout = {	-- Configure your loadouts here!
	[1]	= { 
		{ weapon = "mg_mike4", 		ammo = { 32, "ar2"	  } },
		{ weapon = "mg_m9", 		ammo = { 90, "smg1"	  } },
		{ weapon = "tfa_dod_nambu",	ammo = {100, "pistol" } },
	},
	[2]	= { 
		{ weapon = "weapon_ar2", 	ammo = { 32, "ar2"	  } },
		{ weapon = "weapon_mp5",	ammo = { 90, "smg1"	  } },
		{ weapon = "weapon_deagle",	ammo = {100, "357" 	  } },
	},
	[3]	= { 
		{ weapon = "weapon_stunstick" 	},	-- no ammo
		{ weapon = "tfa_rpg_rocket",	ammo = {3, "rpg_rounds"} },
		{ weapon = "weapon_crossbow",	ammo = {5, "XBowbolt"  } },
		{ weapon = "weapon_tf2_pan" 	},	-- no ammo
		{ weapon = "weapon_smg1" 		},  -- no ammo
	},
	--[[ Example codes
	===> To get all default weapon Ammo Types: https://wiki.facepunch.com/gmod/Default_Ammo_Types
	[4]	= { 
		{ weapon = "<weapon_name>",	ammo = { <amount>, "<ammo type>" } },
	},
	[5]	= { 
		{ weapon = "weapon_ak47",	ammo = { 60, "ar2" } },
		{ weapon = "weapon_m4a1"	}, -- without ammo, optional
	},
	-- ... add more here
	]]
}

hook.Add("PH_OnHunterLoadOut", "Give Test Weapon", function (pl)

	if !pl:Alive() then return end --make sure they're alive
	
	if (SpawnRandomLoadout) then
		local SelectRandomly = Random_Loadout[ math.random( 1, #Random_Loadout ) ]		
		for _,WeaponData in pairs( SelectRandomly ) do
			if (WeaponData.weapon) then ply:Give( WeaponData.weapon ) end
			if (WeaponData.ammo) then ply:GiveAmmo( WeaponData.ammo[1], WeaponData.ammo[2] ) end
		end
	else
		for _,weapon in ipairs(Simple_Loadout) do ply:Give( weapon ) end
	end
	
	return KeepDefaultWeapon
	
end)

Further Explanation
From Random_Loadouts table; To add more weapons, simply copy from following example: (or copy start from no. 4)

[4]	= { 
	{ weapon = "<weapon_name>",	ammo = { <amount>, "<ammo type>" } },
},

and adds as many weapons as you want.

Fields explanation
weapon : weapon class name to be given. (e.g: weapon_ak47)
ammo : Optional, adds additional ammo. The two fields is required (e.g { 99, "ar2" } ) otherwise error will occurs. If the weapon shares similar ammo, just leave it blank without adding ammo={ ... }

All done, and restart your map.

Last Thing to note

Currently this is the only way to add weapons for Hunters in Prop Hunt: X. This doesn't work on Non-Prop Hunt: X gamemodes as they have different method of giving weapons. In Later version, there will be a GUI addon to manage weapon easier.
Make sure not to forget about quotation marks (") and commas (,) when editing them!

Old Methods (**Outdated, Not Recommended**)

File Needed

  1. Go to your Garry's Mod Main server folder and navigate to garrysmod/lua/autorun/server folder. (Multiplayer/LAN Game is the same thing)
  2. Create a new file called sv_hunter_weapons.lua inside the garrysmod/lua/autorun/server folder.

After this you need to choose one method. There's currently 2 methods: 1. Simple Loadouts Method or 2. Random Loadouts Method.

1. [Simple Loadouts Method]

Open the following file sv_hunter_weapons.lua and paste the following code :

-- Add your custom weapons here.
local Weapons = {
	"tfa_your_weapon_names_here",
	"tfa_csgo_ak47",
	"cw2_mp41",
	-- "add more here",
	-- "add more here",
}

--!! Do not touch unless if you're know what you're doing !!--

-- A helper function to give weapon to a player
local function GiveWeapon( ply )
	if ply:Alive() then
		for _,weapon in pairs(Weapons) do ply:Give( weapon ) end
	end
end

-- This hook will be triggered when blind time was over
hook.Add("PH_BlindTimeOver", "Give Hunters some Custom Weapons", function()
	for _,ply in pairs (team.GetPlayers( TEAM_HUNTERS )) do
		GiveWeapon( ply )
	end
end)

Done - Restart your map.

2. [Random Loadouts Method]

This is alternative version for giving weapons that every hunter respawn, the weapons will be randomly given.

Same as above, open the following file sv_hunter_weapons.lua and paste the following code (make sure the file is empty!) :

-- Here's the better version for Random Loadouts, plus with their additional ammo given on spawn
local LoadoutList = {	-- Configure your loadouts here!
	[1]	= { 
		{ weapon = "mg_mike4", 		ammo = { 32, "ar2"	  } },
		{ weapon = "mg_m9", 		ammo = { 90, "smg1"	  } },
		{ weapon = "tfa_dod_nambu",	ammo = {100, "pistol" } },
	},
	[2]	= { 
		{ weapon = "weapon_ar2", 	ammo = { 32, "ar2"	  } },
		{ weapon = "weapon_mp5",	ammo = { 90, "smg1"	  } },
		{ weapon = "weapon_deagle",	ammo = {100, "357" 	  } },
	},
	[3]	= { 
		{ weapon = "weapon_stunstick" 	},	-- no ammo
		{ weapon = "tfa_rpg_rocket",	ammo = {3, "rpg_rounds"} },
		{ weapon = "weapon_crossbow",	ammo = {5, "XBowbolt"  } },
		{ weapon = "weapon_tf2_pan" 	},	-- no ammo
		{ weapon = "weapon_smg1" 		},  -- no ammo
	},
	--[[ Example codes
	===> To get all default weapon Ammo Types: https://wiki.facepunch.com/gmod/Default_Ammo_Types
	[4]	= { 
		{ weapon = "<weapon_name>",	ammo = { <amount>, "<ammo type>" } },
	},
	[5]	= { 
		{ weapon = "weapon_ak47",	ammo = { 60, "ar2" } },
		{ weapon = "weapon_m4a1"	}, -- without ammo, optional
	},
	-- ... add more here
	]]
}

--!! Do not touch unless if you're know what you're doing !!--

local function GiveWeapon( ply )
	-- Randomly select weapons for loadout
	local SelectRandomly = LoadoutList[ math.random( 1, #LoadoutList ) ]
	if ply:Alive() then
		for _,WeaponData in pairs( SelectRandomly ) do
			if (WeaponData.weapon) then ply:Give( WeaponData.weapon ) end
			if (WeaponData.ammo) then ply:GiveAmmo( WeaponData.ammo[1], WeaponData.ammo[2] ) end
		end
	end
end

-- This hook will be triggered when blind time was over
hook.Add("PH_BlindTimeOver", "Give Hunters Random Custom Weapons", function()
	for _,ply in pairs (team.GetPlayers( TEAM_HUNTERS )) do
		GiveWeapon( ply )
	end
end)