[PointShop] Changing category if an item with GM:ShouldDrawLocalPlayer doesn't work like expected

Okay I’ve tried figuring out the problem with the following code for hours, but I guess I’m just too stupid to get it?


ITEM.Name = "item_test"
ITEM.Price = 200
ITEM.Material = "none"
ITEM.WeaponClass = "none"
ITEM.SingleUse = false

function ITEM:OnBuy(ply)
	-- ply:Give(self.WeaponClass)
	-- ply:SelectWeapon(self.WeaponClass)
	ply:PS_ToggleMenu()
end

local function buying(ply)
local has = "Sell"
local hasnot = "Buy"

	if ply:PS_HasItem('item_test') then
		return has
		else
		return hasnot
	end
end
hook.Add( "ShouldDrawLocalPlayer", "item_buy", buying )



local boughtornot = buying()
ITEM.Category = boughtornot

The code is supposed to change the ITEM.Category when it is bought.

But it spits out this error:


[ERROR] lua/pointshop/items/buyable_weapons/item_test.lua:17: attempt to index local 'ply' (a nil value)
  1. buying - lua/pointshop/items/buyable_weapons/item_test.lua:17
   2. unknown - lua/pointshop/items/buyable_weapons/item_test.lua:25
    3. include - [C]:-1
     4. LoadItems - lua/pointshop/sh_init.lua:119
      5. Initialize - lua/pointshop/sh_init.lua:55
       6. unknown - lua/autorun/pointshop.lua:4

Apparently it has a problem with



local boughtornot = buying()

because if I comment it doesn’t give me a error.

Greeting, Santifocus

This code makes no sense, because ITEM.Category is a string and you are trying to set it to bool. If you want to change something when the item is bought, you have to use **ITEM.OnBuy **hook (read the official Pointshop documentation)

my english is bad

No, bool = true/false
buying() = “Sell”/“Buy”

Or rather that is what it should be but apparently because of it being a “hook.Add” it cant return a value into the function (buying())

Yes, i see, sorry. This is how it should look like


function ITEM:OnBuy(ply)
	self.Category = "Sell"
end

function ITEM:OnSell(ply)
	self.Category = "Buy"
end

If it won’t work, you have to change PS.Items ITEM table


local function buying(ply) --has ply as argument
local has = "Sell"
local hasnot = "Buy"

	if ply:PS_HasItem('item_test') then --expects ply, will error if it's nil
		return has
		else
		return hasnot
	end
end
hook.Add( "ShouldDrawLocalPlayer", "item_buy", buying )



local boughtornot = buying() --calling with no arguments, ply will be nil, error

Yeah that does sound like a good idea.

What do you mean by changing PS.Items ITEM table tho?

If it won’t work, just add that line after each category change:


PS.Items[self.ID] = self


function ITEM:OnBuy(ply)
	self.Category = "Sell"
	PS.Items[self.ID] = self
end

function ITEM:OnSell(ply)
	self.Category = "Buy"
	PS.Items[self.ID] = self
end

So how can I do it different? Because apparently it is not possible to draw variables (has/hasnot) out of a block (function)

[editline]10th October 2017[/editline]

Still doesn’t work :’(


ITEM.Name = "item_test"
ITEM.Price = 200
ITEM.Material = "none"
ITEM.WeaponClass = "none"
ITEM.SingleUse = false
ITEM.Category = "Buy"

function ITEM:OnBuy(ply)
	ply:PS_ToggleMenu()
	self.Category = "Sell"
	PS.Items[self.ID] = self
end

function ITEM:OnSell(ply)
	self.Category = "Buy"
	PS.Items[self.ID] = self
end

Did you create those categories? You have to create them before you can actually put items in them, read the official documentation

You mean this one rigth?:

__category.lua


CATEGORY.Name = 'Buy'
CATEGORY.Icon = 'cart_put'
CATEGORY.Order = -1

Well, seems like noone knows how to solve this ._.

What am I supposed to do with that code :frowning:
He just copied my original code and added comments in that are partly not even true.