Ok so i tried to refrain from asking for help because i never learn anything. however, i been working on this code for about 2 days now and i can't get past it.
So here is what it does, You hit E and a random weapon spawns. However, sometimes spawned_weapon comes out and you can't pick it up. Or you get the spawned_weapon model and it allows you to pick it up, but its a random weapon type. I am trying to fix this so when the player hits E on the entity it just spawns a random weapon with the correct model.
Here is my code
[CODE]function ENT:createGun()
gun = ents.Create("spawned_weapon")
self.Once = false
local gun = ents.Create("spawned_weapon")
local rand1 = math.random( 1, 15 )
local rand2 = math.random( 1, 15 )
local rand3 = math.random( 1, 15 )
local rand4 = math.random( 1, 15 )
local rand5 = math.random( 1, 15 )
local rand6 = math.random( 1, 15 )
local rand7 = math.random( 1, 15 )
local rand8 = math.random( 1, 15 )
local rand9 = math.random( 1, 15 )
local rand10 = math.random( 1, 15 )
local rand11 = math.random( 1, 15 )
local rand12 = math.random( 1, 15 )
local rand13 = math.random( 1, 15 )
local rand14 = math.random( 1, 15 )
local rand15 = math.random( 1, 15 )
if rand1 == 1 then
gun:SetModel("models/weapons/w_pist_elite_dropped.mdl")
gun:SetWeaponClass("weapon_real_cs_elites")
end
if rand2 == 2 then
gun:SetModel("models/weapons/w_eq_fraggrenade.md")
gun:SetWeaponClass("weapon_real_cs_grenade")
end
if rand3 == 3 then
gun:SetModel("weapon_real_cs_five-seven")
gun:SetWeaponClass("weapon_real_cs_five-seven")
end
if rand4 == 4 then
gun:SetModel("models/weapons/w_rif_galil.mdl")
gun:SetWeaponClass("weapon_real_cs_galil")
end
if rand5 == 5 then
gun:SetModel("models/weapons/w_rif_m4a1.mdl")
gun:SetWeaponClass("weapon_real_cs_m4a1")
end
if rand6 == 6 then
gun:SetModel("models/weapons/w_shot_m3super90.mdl")
gun:SetWeaponClass("weapon_real_cs_pumpshotgun")
end
if rand7 == 7 then
gun:SetModel("models/weapons/w_shot_xm1014.mdl")
gun:SetWeaponClass("weapon_real_cs_xm1014")
end
if rand8 == 8 then
gun:SetModel("models/weapons/w_snip_sg550.mdl")
gun:SetWeaponClass("weapon_real_cs_sg550")
end
if rand9 == 9 then
gun:SetModel("models/weapons/w_snip_awp.mdl")
gun:SetWeaponClass("weapon_real_cs_awp")
end
if rand10 == 10 then
gun:SetModel("models/weapons/w_pist_deagle.mdl")
gun:SetWeaponClass("weapon_real_cs_desert_eagle")
end
if rand11 == 11 then
gun:SetModel("models/weapons/w_smg_p90.mdl")
gun:SetWeaponClass("weapon_real_cs_p90")
end
if rand12 == 12 then
gun:SetModel("models/weapons/w_eq_flashbang.mdl")
gun:SetWeaponClass("weapon_real_cs_flash")
end
if rand13 == 13 then
gun:SetModel("models/weapons/w_smg_tmp.mdl")
gun:SetWeaponClass("weapon_real_cs_tmp")
end
if rand14 == 14 then
gun:SetModel("models/weapons/w_pist_p228.mdl")
gun:SetWeaponClass("weapon_real_cs_p228")
end
if rand15 == 15 then
gun:SetModel("models/weapons/w_smg_mp5.mdl")
gun:SetWeaponClass("weapon_real_cs_mp5a5")
end
local gunPos = self:GetPos()
gun:SetPos(Vector(gunPos.x, gunPos.y, gunPos.z + 27))
gun.ShareGravgun = true
gun.nodupe = true
gun:Spawn()
self.sparking = false
end[/CODE]
You could make a list of weapons and spawn that weapon on the ground instead of using an entity to mimic a weapon. Either way, it'll work if you want to mimic using spawned_weapon, or spawn the actual weapon.
[code]local WeaponsTable = {
{ model = "blah.mdl", class = "pistol" };
{ model = "blah2.mdl", class = "rifle" };
{ model = "blah3.mdl", class = "shotgun" };
{ model = "blah4.mdl", class = "grenade" };
};
local _random = table.Random( WeaponsTable );
print( _random.model );
print( _random.class );[/code]
Not working for me sir.
Post your code. Saying it's "Not working" won't help us help you what so ever.
Here's a tip, one of the main rules of programming is not to repeat yourself, and to make your code as efficient as possible, next time try to think more about the problem you have (Giving random model) and how you can solve this problem using the two rules I mentioned.
An example is Acecools code, his is efficient and he doesn't repeat himself. Also remember there are numerous ways to do a task.
Sorry, you need to Log In to post a reply to this thread.