LUA help: But T credit in point shop

hi what i am trying to do is make a point shop item that when purchased gives the traitor or deceive 1 credit.

-Matt

–Edit–
im assuming i could use ply:GetCredits() + 1 or something of the kind

You can set credits through playermeta:SetCredits( number )

that sounds really usefull!
how would I get that to add one credit to the current number of credits the player has?

[editline]5th March 2015[/editline]

i have found this code



ITEM.Name = '1 Extra Credit'
ITEM.Price = 100
ITEM.Model = 'models/Items/coin.mdl'
ITEM.NoPreview = true
ITEM.SingleUse = true

function ITEM:OnBuy(ply)
	ply:SetCredits(ply:GetCredits() + 1)
end


but i can buy it more than once a round which i do not want.
any suggestions on how to fix that?

Try this:


ITEM.Name = '1 Extra Credit'
ITEM.Price = 100
ITEM.Model = 'models/items/coin.mdl'
ITEM.NoPreview = true
ITEM.SingleUse = true

function ITEM:OnBuy(ply)
        if ( ply.HasBoughtCredit ) then 
                ply:ChatPrint( "You have already bought a credit this round!" ) 
                return false 
        end
	ply:SetCredits( ply:GetCredits() + 1 )
        ply.HasBoughtCredit = true
end

You will have to erase that variable in some pre-round TTT hook.

i know i am basically dragging this out of you and for that i am sorry.
how would i clear that variable preround?

Use one of these: http://ttt.badking.net/guides/hooks

Just loop through all the players and set ply.HasBoughtCredit to nil.

yeah i dont know how to do that but i will find a way thank you for your help.
this is what i am trying. would i put this hook in a lua/autorun folder?



function TTTBeginRound (ply)
	ply.HasBoughtCredit = false
end



[editline]5th March 2015[/editline]

it will also still take your money if you have already purchased the item

this is what i have now. Will test



hook.Add("TTTBeginRound", "creditremoval", function(ply)
	player.GetAll(ply.HasBoughtCredit = nil)
end)


player.GetAll returns a table of all connected players. You should use it with a for loop. Try this inside the hook:


for _, v in pairs( player.GetAll() ) do
        v.HasBoughtCredit = nil
end

this is what i have now. is this what you were thinking?



hook.Add("TTTBeginRound", "creditremoval", function(ply)
	for _, v in pairs( player.GetAll() ) do
        v.HasBoughtCredit = nil
end)


if it is i am getting this error



[ERROR] addons/pointshop/lua/autorun/pointshop.lua:12: ')' expected (to close '(' at line 6) near '<eof>'
  1. unknown - addons/pointshop/lua/autorun/pointshop.lua:0


You need another end for the for loop

like this?



hook.Add("TTTBeginRound", "creditremoval", function(ply)
	for _, v in pairs( player.GetAll() ) do
        v.HasBoughtCredit = nil
    end
end)


[editline]5th March 2015[/editline]

this works perfectly thank you so much.

although if you try and but it a second time it will take more money out. how would i fix this? also can i restrict it to the traitor/detective to buy.

Try this for your item code:


ITEM.Name = '1 Extra Credit'
ITEM.Price = 100
ITEM.Model = 'models/items/coin.mdl'
ITEM.NoPreview = true
ITEM.SingleUse = true

function ITEM:CanPlayerBuy( ply )
        if ( ply.HasBoughtCredit ) then 
                ply:ChatPrint( "You have already bought a credit this round!" ) 
                return false 
        end
        return true
end

function ITEM:OnBuy( ply )
	ply:SetCredits( ply:GetCredits() + 1 )
        ply.HasBoughtCredit = true
end

what was that supposed to fix?

Stops it from deducting points if they can’t buy it