[TTT] Choosing a random View/World model on purchase

I am trying to have a weapon where when you buy the gun from the Equipment shop, it changes the view and world model to something chosen. So is this even possible or am I just wasting my time? I dont need the weapon to fire properly or anything because the weapon is going to kill the player upon shooting it.



SWEP.ViewModel  = "models/weapons/v_rif_ak47.mdl"
SWEP.WorldModel = "models/weapons/w_rif_ak47.mdl"
SWEP.CanBuy = {ROLE_TRAITOR} 

-- M16 and AK use different paths. Fix this

weaponmodels = {
"pist_glock18", "pist_fiveseven", "pist_deagle", "snip_scout", "shot_xm1014", "mach_m249para",
"smg_mac10", 
}

hook.Add("TTTOrderedEquipment", "RandomizeWeapon", function(ply, equipment, is_item)
local entry = table.Random(weaponmodels)

print("entry is " .. entry)
print(weaponmodels)

self.ViewModel			= "models/weapons/cstrike/c_".. entry ..".mdl"
self.WorldModel			= "models/weapons/w_".. entry ..".mdl"
end)



And I get this lua error upon buying the weapon



concommand.lua]

[ERROR] addons/exhos ttt weps/lua/weapons/weapon_ttt_trap.lua:62: attempt to index global 'self' (a nil value)
  1. v - addons/exhos ttt weps/lua/weapons/weapon_ttt_trap.lua:62
   2. Call - lua/includes/modules/hook.lua:84
    3. unknown - gamemodes/terrortown/gamemode/weaponry.lua:421
     4. unknown - lua/includes/modules/concommand.lua:69



@it changes the view and world model to something chosen.
Do you want to change the world and view model of an existing gun to something else? Sure, it’s possible.

You are setting a variable however, which isn’t used after creation for this.

I want to set the view and world model to what is chosen in the table.Random. I just can’t figure out how to make it change

“self” is only passed (implicitly) within a ENT:Method() function, so I believe that you should use SWEP.ViewModel. Also, since you will sometimes have multiple weapons active, keep in mind the server only utilises a single file, and so only a single hook will be used, meaning that the hook will only run on the last weapon that was bought, since you’re overriding the hook every time - the same identifiers are used.
Something like this should be what you’re looking for:
[lua]SWEP.ViewModel = “models/weapons/v_rif_ak47.mdl”
SWEP.WorldModel = “models/weapons/w_rif_ak47.mdl”
SWEP.CanBuy = {ROLE_TRAITOR}

– M16 and AK use different paths. Fix this

local weaponmodels = { – Localize. You could override other variables with the same name
“pist_glock18”,
“pist_fiveseven”,
“pist_deagle”,
“snip_scout”,
“shot_xm1014”,
“mach_m249para”,
“smg_mac10”,
}

function SWEP:Initialize()
– now you can use self, also, you should only do this in Initialize() otherwise
– a hook will be added whenver this file is run, which might not be when a weapon
– is spawned
hook.Add( “TTTOrderedEquipment”, “RandomizeWeapon” … self:EntIndex(), function(ply, equipment, is_item)
– the identifier is now unique for every weapon

	local mdl = table.Random( weaponmodels )
	self.ViewModel			= "models/weapons/cstrike/c_".. entry ..".mdl"
	self.WorldModel			= "models/weapons/w_".. entry ..".mdl"

end )

end )

function SWEP:OnRemove()
– remove the hook when the weapon is removed, otherwise you’ll get “attempt to index nil value” or “NULL Entity” errors
– when those hooks are run
hook.Remove( “TTTOrderedEquipment”, “RandomizeWeapon” … self:EntIndex() )

end[/lua]

I am getting errors because your code uses the ENT library in a SWEP

Sorry about that, force of habit. Fixed the code.

Why do you bother with hooks? When you buy a weapon, SWEP:Initialize will be called once the SWEP created.

Why do you even bother with making the same gun have multiple looks, just make a few different guns with different classes and give a random one to the player from that hook?

You are overcomplicating things where you shouldn’t.