Got a 3D2D Button, but how to run a function from another file?!

I am not sure if I am getting the function wrong or what but, this is for a money printer and I have been playing around with 3D2D stuff and managed to add buttons that work! It is just this is in the cl_init.lua:



surface.SetDrawColor(0, 0, 0, 100)
if (tr.Entity == self.Entity) and pos.x < 1.5 and pos.x > -1.5 and pos.y < 7.5 and pos.y > -7.5 then
	--Change the color of the button when we look at it.
	surface.SetDrawColor(0, 0, 0, 220)	
	--This is where all the good shit happens, like taking money or, whatever happens when you press the button
	if (player:KeyDown(IN_USE)) and self:GetNWInt("PrintA") >= 1 then
		surface.SetDrawColor(0, 0, 0, 240)
	end
end
surface.DrawRect(-38, -7, 76, 15)
draw.SimpleText("COLLECT", "ChatFont", 0, 0, Color(255, 255, 255, alpha_on), 1, 1)
surface.SetDrawColor(0, 0, 0, 255)
surface.DrawOutlinedRect(-38, -7, 76, 15)


So, in the second if statement, what happens is when the player presses E on the button it changed the button color (I have also tested with “print (“test”)” and that worked in console. But the problem is I am having trouble with the collecting of the money. Originally there is this in the init.lua (not cl_init.lua where the button is):



function ENT:Use(activator)
	if(activator:IsPlayer()) and self:GetNWInt("PrintA") >= 1 then
	    activator:addMoney(self:GetNWInt("PrintA"));
	    DarkRP.notify(activator, 0, 4, "You have collected $"..self:GetNWInt("PrintA").." from the printer.") --What it says when you collect your money
	    self:SetNWInt("PrintA",0)
	end
end


So this is the original printer “take money” function. And when the player presses E anywhere on the printer it will give them the money. However, I am stuck on making a function for collecting the money when the button (in cl_init.lua) is pressed.

Thanks if you can help!

EDIT
I also have the AddCSLuaFile(“cl_init.lua”) and stuff

just make the button run “self:Use()”

Thanks for the reply, yeah I guess I could do that. However, the Use() function will still be there so when you press E on the printer it will collect it.

It is more of making a new function which I have not had success with. I have tried:



function collect(ply)
    ply:addMoney(self:GetNWInt("PrintA"));
end


By the way, the GetNWInt(“PrintA”) is just the amount of money for example it could be 300 or 400, I just use it for other things.

But then in the cl_init.lua file I guess I could just call the collect function which would be self:collect()?

you could give the Use function an argument, and check “if not argument then return end”, and with the button you give the argument.

Hmmm, yeah I could use a NWBool?!

So:



    --create a NWBool defaulting at false
    self:SetNWBool(collectBtn, false)
   
    --Now this to go in the button
    if (player:KeyDown(IN_USE)) and self:GetNWInt("PrintA") >= 1 then
        surface.SetDrawColor(0, 0, 0, 240)
        self:SetNWBool(collectBtn, true)
    end


Then in the init.lua I could do



function ENT:Use(activator)
    if(self:GetNWBool(collectBtn) == true and self:GetNWInt("PrintA") >= 1 then
        activator:addMoney(self:GetNWInt("PrintA"));
	self:SetNWInt("PrintA",0)
        --I added this underneath to set the NWBool back to false one E is pressed on the printer and to make it active again you have to press the button.
        self:SetNWBool(collectBtn, false)
	end
end


So I guess the money will only be added if the NWBool(collectBtn) is on true and when E is pressed on the printer it gets set to false, however if it is pressed with the button, first the button will set NWBool(collectBtn) to true and then run the USE function setting it to false straight after.

This seems like a sketchy way of doing it but I will try it when I get home.

Any other suggestions?! - Thanks

No, I meant you make instead of “ENT:Use(activator)” > “ENT:Use(activatior, bool)” and then do “if not bool then return end” above the “self:GetNWInt” check.
and in your button, you do “self:Use(player, true)”.

Ahhhhhhhhhh right, I get you now I will try it when I get home. Thanks

Just tried it, did not work. Any other ideas? I am all out now :confused: