NPCs dropping items

I’m looking for a code that when I kill an NPC like a zombie, there is a chance that he’ll drop a random weapon or item (based on a set list of weapons and items I could edit).

Thanks in advance

You use the OnNPCKilled hook, then create a gun when they are killed, you can use [entity.GetClass

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?search=entity.GetClass) to check for certain npcs if you want only certain ones dropping weapons.

[lua]
if not SERVER then return end

local WeaponList = {
“weapon_pistol”,
“weapon_smg1”,
“weapon_crowbar”
}

hook.Add(“OnNPCKilled”, “DropWeaponOnNPCKilled”, function(npc, killer, weapon)

local weapon = ents.Create(table.Random(WeaponList)) --randomly choose a weapon entity from WeaponList
weapon:SetPos(npc:LocalToWorld(npc:OBBCenter())) -- Set the position to the center of the NPC
weapon:Spawn()

end)
[/lua]

Wouldn’t you need to set the world model for that as well?

No, weapons have those built in.

Ok, so I tried to add a probability to it, but it didn’t work.


if not SERVER then return end

local AmmoList = {
	"item_ammo_pistol",
	"item_ammo_smg1",
	"item_ammo_ar2",
	"item_ammo_357",
	"item_ammo_crossbow",
	"item_box_buckshot",
	"item_rpg_round"
}
	
local LowList = {
	"item_battery",
	"item_healthkit",
	"item_healthvial",
	"weapon_pistol",
	"weapon_smg1",
	"weapon_frag"
}

local MedList = {
	"weapon_ar2",
	"weapon_shotgun",
	"item_ammo_ar2_altfire",
	"item_ammo_smg1_grenade"
}
	
local HighList = {
	"weapon_357",
	"weapon_crossbow",
	"weapon_rpg",
	"weapon_physcannon"
}

hook.Add("OnNPCKilled", "DropWeaponOnNPCKilled", function(npc, killer, weapon)
	
	chance = math.random(1,100)
	if chance = (1,50) then return end
	if chance = (51,70) then
		local weapon = ents.Create(table.Random(AmmoList)) 
		weapon:SetPos(npc:LocalToWorld(npc:OBBCenter())) 
		weapon:Spawn()
	end
	if chance = (71,85) then
		local weapon = ents.Create(table.Random(LowList)) 
		weapon:SetPos(npc:LocalToWorld(npc:OBBCenter())) 
		weapon:Spawn()
	end
	if chance = (86,95) then
		local weapon = ents.Create(table.Random(MedList)) 
		weapon:SetPos(npc:LocalToWorld(npc:OBBCenter())) 
		weapon:Spawn()
	end
	if chance = (96,100) then
		local weapon = ents.Create(table.Random(HighList)) 
		weapon:SetPos(npc:LocalToWorld(npc:OBBCenter())) 
		weapon:Spawn()
	end
end

I’m completely new to lua, and I have been reading the lua wiki for gmod. If you don’t mind, please be a bit descriptive in an answer.

Well for one thing ‘=’ means you are setting a value, such as ‘x = 10’. To check something you’ll need to use ‘==’ a double equal sign. I suggest you read Programing in Lua (PIL) to get a basic idea of how Lua syntax works.

As for creating chances I haven’t really tried that before. So I don’t know a good way of going about it.

This is a NPC drop script WITHOUT tables. This is because it’s in the early stages of a LUA tutorial on how
to make a gamemode called “cakemod”. Tables are for optimizing and saving time.

This is a serverside script (init.lua)

Video is bad for tutorials.

[lua]if CLIENT then return end

local AmmoList = {
“item_ammo_pistol”,
“item_ammo_smg1”,
“item_ammo_ar2”,
“item_ammo_357”,
“item_ammo_crossbow”,
“item_box_buckshot”,
“item_rpg_round”
}

local LowList = {
“item_battery”,
“item_healthkit”,
“item_healthvial”,
“weapon_pistol”,
“weapon_smg1”,
“weapon_frag”
}

local MedList = {
“weapon_ar2”,
“weapon_shotgun”,
“item_ammo_ar2_altfire”,
“item_ammo_smg1_grenade”
}

local HighList = {
“weapon_357”,
“weapon_crossbow”,
“weapon_rpg”,
“weapon_physcannon”
}

hook.Add(“OnNPCKilled”, “DropWeaponOnNPCKilled”, function(npc, killer)
local rndweapon = nil
chance = math.random(1,100)
if chance < 50 then return end
if chance > 51 && chance < 70 then rndweapon = table.Random(AmmoList) end
if chance > 71 && chance < 85 then rndweapon = table.Random(LowList) end
if chance > 86 && chance < 95 then rndweapon = table.Random(MedList) end
if chance > 96 && chance < 100 then rndweapon = table.Random(HighList) end
weapon = ents.Create(rndweapon)
weapon:SetPos(npc:LocalToWorld(npc:OBBCenter()))
weapon:Spawn()
end
[/lua]

Fixed copy of your script.

[editline]12:19PM[/editline]

Why would you do it without? It’s just time wasting.

I tried this, and it didn’t work either

Why don’t you use smaller numbers (or decimals) to get a chance-percentage?

Tell us what’s wrong then maybe? You can’t expect us to fix it without you telling us what the error is.

I think the problem is of mismatched parenthesis. Replace line 47 from iRzilla’s script with “end)”.