Adding player money in a function called from a 3D2D button

So, I have made a 3D2D button which is great! (This is in cl_init.lua). I have also used the Net library to be able to run a function in the init.lua from the cl_init.lua button which is great! The cl_init.lua looks like this:



if (player:KeyDown(IN_USE)) then
    net.Start("Collect")
    net.SendToServer()
    --Below will change the color of the button when it is pressed
    surface.SetDrawColor(0, 0, 0, 240)
end


So this what happens when the button is pressed, it then goes over to init.lua which is:



function collectBtn()
	self:addMoney(300);
end

net.Receive("Collect", collectBtn)


NOTE: I know the function works because I can do a print(“test”) and I can see it in console. So, how would I go about adding money to the player?

This “self:addMoney()” does not work, is this because it is client side. I have been told I can achieve what I am trying to do with net.WiteEntity however I am not sure how to use it?

Normally, I would of used:



function ENT:Use(activator)
	if(activator:IsPlayer()) then
	    activator:addMoney(300);
	end
end


This will give the player $300 when they press E on the printer. But obviously I have the button now.
Thanks in advance!

Why don’t you use theENTITY:Use hook instead, on the printer which is called only server side and destroys potential exploits?

Just edited it, look at last block of code. (My bad) I used ENT:Use but that was just for pressing E on the whole entity. I am just trying to make the button press give the money.

net.Receive’s function takes two arguments: length, and player. Use the second argument to see who sent the message.

[editline]5th August 2015[/editline]

You should also be checking if the player is able to press the button serverside. If you leave it like that I could give myself infinite money with just console commands.

[editline]5th August 2015[/editline]


lua_run_cl for i = 1, 100 do net.Start("Collect") net.SendToServer() end

-snip-
2slow :c
Do what Ott says.

Use Vector:Distance for checking if player is near printer and within range.

Question: Does this printer generate money or gives 300 and its stash is infinite?

Ahh right okay, thanks for that. So instead of net.Start(“Collect”) I would do what you say?

EDIT, just tried it and it is giving me errors: “’=’ expected near ‘for’” Just wondering if you know, I have not even started to touch stuff like this (like for i = 1) and for and stuff

[editline]5th August 2015[/editline]

The generation of the money is fine. It is just when the player press E on the button to collect it, I am not sure how to add $300 to the players balance.

[editline]5th August 2015[/editline]

Oh right, I was using it as the “name” for the first bit, then for the second, the function name.

No, that was the code I could use to break your thing. Just add 2 arguments to collectBtn.

Oh my bad, I misunderstood.

How would you go about checking if you are able to press it server side?

I would probably net.WriteEntity the printer, then check serverside if the entity is a printer (ent:GetClass() == “whatever”) and if the player is near (ply:GetPos():Distance(ent:GetPos()) < 100) and looking at it (ply:GetEyeTrace().Entity == ent).

Ahh okay thanks, I will play around with it.

Any idea on how to give the player money on button press?

addMoney

Hehe, yeah I tried that:



function collectBtn()
	self:addMoney(300);
end


Self doesn’t exist in that function

Yeah, I see that now thanks. Because in the normal version of the printer (when you just pressed E on it), it had function ENT:Use(activator). Then to add the money it was activator:addMoney(“300”).

I am just wondering how to add money to the player in my collectBtn function.

Add 2 parameters to collectBtn: length, and ply. Ply will be the player.