Money + Derma Menu with objects.

Is it possible to make a Money system that gives you money to buy weapons and props in a Derma Menu killing the enemy? If yes, how? I tried and failed… that’s why I’m here asking your help!

for money use:

for derma use:
http://wiki.garrysmod.com/?title=Derma_Tutorial2 - basics of derma
http://www.facepunch.com/showthread.php?t=543184 - big derma guide

Cough usermessages Cough

Seriusly, what the fuck is that? The only good thing is the “big guide”, that’s quite useful.
About money, I totally don’t know what it has to do with moneys.

forgot that link that should show you a bit better

No, no no. Never use them unless you feel like being really lazy.

Instead, use usermessages

Uhm… Yeah but now how do I use usermessages to make moneys? Also how to make a derma menu that removes moneys?

All in all “Money” is just a value you set on the server and of which you give a copy to the client. It could be as easy as ply.Money = 10. If you want physical money just make an entity with the money model that adds it’s “money” value to the player’s money when used. For saving you could use text files, but I suggest using the builtin sqlite database. http://wiki.garrysmod.com/?title=Sql

You need to learn lua before even trying this

So I can make an entity, “Money”, then make another entity that gives people the main “Money” entity, right?
If that’s right it means I can simply make people drop entities on death so to make people able to get money by killing, or better:

[lua] if ( attacker:IsValid() && attacker:IsPlayer() ) then
if ( attacker == ply ) then
attacker:AddFrags( -1 )
ply:ConCommand( “remove money entity” )
else
attacker:AddFrags( 1 )
ply:ConCommand( “add money entity” )
end
end[/lua]
right?

For one, attacker will never be equal to ply unless you make it equal.

Secondly, you wouldn’t remove or add entities clientside

Thirdly, you would use a variable rather than an entity, and then use the entity to display the variable in some way.

How do I make it equal? And what’s the other way?

Ok, I got it.

This is as basic a money system can get , someone correct me if I’m wrong as I’ve just written it without testing it :

[release]lua/autorun/money.lua[/release]
That is the base of your system, it doesn’t include any saving as of yet (You didn’t make it clear you needed saving so I didn’t bother :smile:) but it would be really easy to add.
You can effectively set any player’s money and notify them of the change and that’s all that matters.
[lua]AddCSLuaFile(“autorun/client/cl_money.lua”) – Having the client download and run the clientside part

– Defining script values
local Starting_Amount = 100

– Setting up the Money variable
hook.Add(“PlayerInitialSpawn”,“MoneySetup”, function(ply)
ply.Money = Starting_Amount
end)

– Getting the metatable Player so we can add functions
local pMeta = FindMetaTable(“Player”)

– This function changes the amount of money a player has. Use it only when you want a set value
-----------USAGE-------------
– ply:SetMoney(ammount) ----

function pMeta:SetMoney(amount)
if amount < 0 then amount = 0 end – That way you can’t have negative money.
self.Money = amount
SendUserMessage(“ClientMoneyUpdate”, self, amount) – Notify the client their money has changed.
end[/lua]

[release]lua/autorun/client/cl_money.lua[/release]
Updates the money for the player so it can be used in clientside scripts.
[lua]-- Setting the player’s money clientside so it can be used in clientside scripts.
function ClientMoneyUpdate( um )
LocalPlayer().Money = um:ReadShort()
end
usermessage.Hook(“ClientMoneyUpdate”, ClientMoneyUpdate)[/lua]

Then you can add some convenience functions, if you want to use these add them at the end of the previous files :

[release]Serverside (money.lua) [/release]
This is just to make it simpler to give and take money.
[lua]-- This is just a convenience function, use it to give money
-----------USAGE-------------
– ply:GiveMoney(amount) —

function pMeta:GiveMoney(amount)
self:SetMoney(self.Money + amount)
end

– This is just a convenience function, use it to take money
-----------USAGE-------------
– ply:TakeMoney(amount) —

function pMeta:TakeMoney(amount)
self:SetMoney(self.Money - amount)
end[/lua]

[release]Clientside (cl_money.lua) [/release]
This is an example of how to display how much money a player has.
[lua]-- Defining script values
local Starting_Amount = 100 – So the HUD has something to display before the first ClientMoneyUpdate.

– A simple hook to display the money we have.
hook.Add(“HUDPaint”,“DrawMoney”,function()
if LocalPlayer:Alive()
draw.RoundedBox( 2, 25, ScrH() - 100, 130, 23, Color( 128, 255, 128, 255 ) )
draw.DrawText("Money: "…LocalPlayer().Money or Starting_Amount, “ScoreboardText”, 30, ScrH() - 97, Color(255,255,255,255),0)
end
end)
[/lua]

:eng101:

Are you serius “Crazy Quebec”?! This is fucking awesome! I’m gonna check this out!

Nice Crazy Quebec this might be useful for what im doing

So, about the Derma Menu:
[lua]function WhenGameStarts()
BuildYourPanel() – Make sure the frame is built when we start. Refer to the timer.Simple() bellow
end

function BuildYourPanel()

YourFrame = vgui.Create( "DFrame" )   
YourFrame:SetTitle( "Derma Testing Stuff" )   
YourFrame:ShowCloseButton( false ) -- Make sure no one presses the close button and get UBER error spam   
YourFrame:SetPos( 50, 50 )   
YourFrame:SetSize( 250, 250 )   
YourFrame:SetDraggable( false ) -- Make sure they can't move it   

local DermaButton = vgui.Create( “DButton”, DermaPanel )
DermaButton:SetText( “Spawn an entity” )
DermaButton:SetPos( 25, 50 )
DermaButton:SetSize( 150, 50 )
DermaButton.DoClick = function()
RunConsoleCommand( “ent_create ent_blastdoor” ) – What happens when you left click the button
end
DermaButton.DoRightClick = function()
RunConsoleCommand( “say I want cheese!” ) – What happens when you right-click the button
end

YourFrame:SetVisible( false )   
gui.SetMousePos( ScrW() / 2, ScrH() / 2 )   

end

timer.Simple( 5, function() – Make a timer when the game starts so we don’t get errors
WhenGameStarts()
BuildYourPanel() – Make the panel itself
end )

function HidePanel() – Function that hides the panel
if YourFrame then – Validate that our frame is there
YourFrame:SetVisible( false )
end
gui.EnableScreenClicker( false )
end

function ShowPanel() – Function that opens the panel
if YourFrame then – Validate that our frame is there
YourFrame:SetVisible( true )
end
gui.EnableScreenClicker( true )
end

function KeyPressed(ply, key)
if key == IN_WALK then
ShowPanel()
end
end
hook.Add( “KeyPress”, “KeyPressedHook”, KeyPressed )

function KeyRelease(ply, key)
if key == IN_WALK then
HidePanel()
end
end
hook.Add( “KeyRelease”, “KeyReleasedHook”, KeyRelease )[/lua]
It seems to work, but the button won’t disappear :confused: it also isn’t in line with the menu :confused:

For the Derma I don’t know but you can’t do that! That will only work for you in singleplayer and when hosting locally, and you obviously don’t want to give your players that kind of access. You’ll need to create a function linked to another console command to do that.

Oh… ok.
Anyway I changed the command to “kill” just to test, still, the button doesn’t disappear :confused:

That’s because you’re not telling it to disappear. Besides if it did, how would you click it again? Do you mean the DPanel?

[lua]function WhenGameStarts()
BuildYourPanel() – Make sure the frame is built when we start. Refer to the timer.Simple() bellow
end

function BuildYourPanel()

local DermaButton = vgui.Create( “DButton”, DermaPanel )
DermaButton:SetText( “Suicide” )
DermaButton:SetPos( 800, 800 )
DermaButton:SetSize( 150, 50 )
DermaButton.DoClick = function()
RunConsoleCommand( “kill” ) – What happens when you left click the button
end
DermaButton.DoRightClick = function()
RunConsoleCommand( “say I want cheese!” ) – What happens when you right-click the button
end

DermaButton:SetVisible( false )   
gui.SetMousePos( ScrW() / 2, ScrH() / 2 )   

end

timer.Simple( 5, function() – Make a timer when the game starts so we don’t get errors
WhenGameStarts()
BuildYourPanel() – Make the panel itself
end )

function HidePanel() – Function that hides the panel
if DermaButton then – Validate that our frame is there
DermaButton:SetVisible( false )
end
gui.EnableScreenClicker( false )
end

function ShowPanel() – Function that opens the panel
if DermaButton then – Validate that our frame is there
DermaButton:SetVisible( true )
end
gui.EnableScreenClicker( true )
end

function KeyPressed(ply, key)
if key == IN_WALK then
ShowPanel()
end
end
hook.Add( “KeyPress”, “KeyPressedHook”, KeyPressed )

function KeyRelease(ply, key)
if key == IN_WALK then
HidePanel()
end
end
hook.Add( “KeyRelease”, “KeyReleasedHook”, KeyRelease )[/lua]
When I press the Walk Button, instead of showing and removing the Button, it just shows and removes the pointer.