Ammo Crate

ive been trying all day to make a AmmoCrate. entity for rp that when you click use on it you would obtain 20 Pistol ammo ive been trying for 6hrs today without luck i hope theres some help out there

[LUA]
function SpawnAmmo( ply )

barrel = ents.Create(“prop_physics”)
barrel:SetModel(“models/items/ammocrate_rockets.mdl”)
barrel:SetPos(ply:GetEyeTrace().HitPos)
barrel:Spawn()
function barrel:Use( ply )

if ply:IsValid() and ply:Alive() then
ply:GiveAmmo( 20, "pistol" )
end

end
end
concommand.Add(“AmmoBarrel”, SpawnAmmo)
[/LUA]

Are there any errors?

uh-uh… that is not a SENT. code that as SENT and try again. that’ll works

how do i code it as SENT. Im pretty damn new at Lua

just look sent_ball SENT. or go http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index7a06.html?title=Lua_Tutorial_Series for some example.

This one is a bit more advanced than what you wanted, but I’m sure you can take out what you need. I made it for my server.

[lua]
AddCSLuaFile(“cl_init.lua”)
AddCSLuaFile(“shared.lua”)

include(“shared.lua”)

function ENT:Initialize()
self.Entity:SetModel(“models/Items/ammocrate_smg1.mdl”)
self.Entity:PhysicsInit(SOLID_VPHYSICS)
self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
self.Entity:SetSolid(SOLID_VPHYSICS)
self.Entity:SetUseType(SIMPLE_USE)
local phys = self.Entity:GetPhysicsObject()
if phys and phys:IsValid() then
phys:Wake()
phys:SetMass(1)
end
self.sparking = false
end

function ENT:SalePrice(activator)
local owner = self.Entity.dt.owning_ent
local discounted = math.ceil(75 * 0.82)

if activator == owner then
	-- If they are still gundealer, sell them the ammo at the discounted rate
	if activator:Team() == TEAM_GUN then
		return discounted
	else -- Otherwise, sell it to them at full price
		return math.floor(75)
	end
else
	return 75
end

end

ENT.Once = false
function ENT:Use(activator,caller)
local owner = self.Entity.dt.owning_ent
self.user = activator
if not activator:CanAfford(self:SalePrice(activator)) then
Notify(activator, 1, 3, “You do not have enough money to purchase ammo!”)
return “”
elseif not self.Once then
self.Once = true
self.sparking = true

	local discounted = math.ceil(75 * 0.82)
	local cash = 75

	activator:AddMoney(cash * -1)
	owner:AddXP(25)
	Notify(owner, 2, 3, "You have gained 25XP for selling Ammo!")
	Notify(activator, 0, 3, "You have purchased ammo for " .. CUR .. tostring(cash) .. "!")
	if activator ~= owner then
		local gain = 0
		if owner:Team() == TEAM_GUN then
			gain = math.floor(75 - discounted)
		else
			gain = math.floor(75)
		end
		if gain == 0 then
			Notify(owner, 2, 3, "You sold some ammo but made no profit!")
		else
			owner:AddMoney(gain)
			local word = "profit"
			if gain < 0 then word = "loss" end
			Notify(owner, 0, 3, "You made a " .. word .. " of " .. CUR .. tostring(math.abs(gain)) .. " by selling ammo!")
		end
	end
	timer.Create(self.Entity:EntIndex() .. "ammo", 1, 1, self.createammo, self)
	end

end

function ENT:createammo()
activator = self.user
self.Once = false
ammo = activator:GiveAmmo( 20, “AR2” ), activator:GiveAmmo( 50, “SMG1” ), activator:GiveAmmo( 25, “Pistol” ), activator:GiveAmmo( 25, “Buckshot” ), activator:GiveAmmo( 25, “357” )
self.sparking = false
end

function ENT:Think()
if self.sparking then
local effectdata = EffectData()
effectdata:SetOrigin(self.Entity:GetPos())
effectdata:SetMagnitude(1)
effectdata:SetScale(1)
effectdata:SetRadius(2)
util.Effect(“Sparks”, effectdata)
end
end

function ENT:OnRemove()
timer.Destroy(self.Entity:EntIndex())
local ply = self.Entity.dt.owning_ent
if not ValidEntity(ply) then return end
end
[/lua]

This is an entity, not a SWEP. In DarkRP this is placed in entitys/entitys/ammo_crate/init.lua

WOAH. that seems advanced >.<

[editline]5th February 2012[/editline]

Thanks ill take a look

I can help you with this as I’m going to make similiar ammo crates for myself.
The only difference is that they will break and spew a bit of ammo like the crates found in hl2 campaign.
You can add me on steam so I’ll help you out.