How would I go about randomly giving a weapon to a user on pointshop purchase
3 replies, posted
Could you simply guide me on what to use? I'd like to try to code it myself.
Could I simply make a table with the weapon class names, randomize it with math.random, use tonumber and use Give()?
I'm trying to create this in /garrysmod/gamemodes/terrortown/entities/weapons which will randomize the weapon, and use that weapon class for the pointshop.
Here's my crappy attempt at it.
/garrysmod/gamemodes/terrortown/entities/weapons
[CODE]SWEP.UseHands = true
SWEP.ViewModelFlip = false
local randomweps = {
["M16"] = "weapon_ttt_m16",
["Pump Shotgun"] = "weapon_ttt_pump",
["Rifle"] = "weapon_zm_rifle"
}
for _, class in pairs(randomweps) do
--local ply = self.GetOwner()
print(_)
print(class)
-- ply:Give(class)
break
end
[/CODE]
pointshop file:
[CODE]ITEM.Name = 'Random Perma Primary (test)'
ITEM.Price = 2000000
ITEM.Model = 'models/props_junk/GlassBottle01a.mdl'
ITEM.WeaponClass = 'permweapon'
ITEM.SingleUse = false
function ITEM:OnBuy(ply)
ply:Give(self.WeaponClass)
end
function ITEM:OnEquip(ply)
ply:Give(self.WeaponClass)
end
function ITEM:OnHolster(ply)
ply:StripWeapon(self.WeaponClass)
end
function ITEM:OnSell(ply)
ply:StripWeapon(self.WeaponClass)
end
function ITEM:PlayerSpawn(ply)
ply:Give(self.WeaponClass)
end[/CODE]
Now for the first file it'll print out a weapon but when I uncomment those lines I get:
[CODE][ERROR] gamemodes/terrortown/entities/weapons/permweapon.lua:15: attempt to index global 'ply' (a nil value)
1. fn - gamemodes/terrortown/entities/weapons/permweapon.lua:15
2. unknown - addons/ulib/lua/ulib/shared/hook.lua:110
3. Spawn - [C]:-1
4. SpawnForRound - gamemodes/terrortown/gamemode/player_ext.lua:295
5. SpawnWillingPlayers - gamemodes/terrortown/gamemode/init.lua:564
6. unknown - gamemodes/terrortown/gamemode/init.lua:415[/CODE]
Also, I'll add that it'll spam the print on round start so I'm guessing it's thinking every player gets it.
You can try something like this [code]ITEM.Name = 'Random Perma Primary (test)'
ITEM.Price = 2000000
ITEM.Model = 'models/props_junk/GlassBottle01a.mdl'
ITEM.SingleUse = true
// Contents pointshop file names (ITEM.IDs) not weapon classes
ITEM.Contents = {
"weapon1",
"weapon2",
"weapon3",
"weapon4"
}
function ITEM:CanPlayerBuy(ply)
for _, weapon in pairs(self.Contents) do
if not ply:PS_HasItem(weapon) then
return true
end
end
return false
end
function ITEM:OnBuy(ply)
local randomedContents = table.sort(self.Contents, function(a, b) return math.random(0, 1) > 0.5 end)
for _, weapon in pairs(randomedContents) do
if not ply:PS_HasItem(weapon) then
ply:PS_GiveItem(weapon)
ply:PS_EquipItem(weapon)
return
end
end
end[/code]
[QUOTE=iJohnny;51646659]You can try something like this [code]ITEM.Name = 'Random Perma Primary (test)'
ITEM.Price = 2000000
ITEM.Model = 'models/props_junk/GlassBottle01a.mdl'
ITEM.SingleUse = true
// Contents pointshop file names (ITEM.IDs) not weapon classes
ITEM.Contents = {
"weapon1",
"weapon2",
"weapon3",
"weapon4"
}
function ITEM:CanPlayerBuy(ply)
for _, weapon in pairs(self.Contents) do
if not ply:PS_HasItem(weapon) then
return true
end
end
return false
end
function ITEM:OnBuy(ply)
local randomedContents = table.sort(self.Contents, function(a, b) return math.random(0, 1) > 0.5 end)
for _, weapon in pairs(randomedContents) do
if not ply:PS_HasItem(weapon) then
ply:PS_GiveItem(weapon)
ply:PS_EquipItem(weapon)
return
end
end
end[/code][/QUOTE]
Would it be possible with weapon classes? I don't have any weapons in the pointshop at the moment. Or if there's a way to somehow "hide" it from the pointshop I can do that so it registers the ID's.
Edit: Nvm I'll make a hidden category.
Either way, thank you. I'll try it out in a bit.
Edit 2: Hmm Nothing happens when purchasing/equipping this.
Also I noticed it gives a random weapon in the pointshop, I meant a random weapon class on spawn with it but I like the idea and I prefer that one haha
Sorry, you need to Log In to post a reply to this thread.