So I have just started Lua coding recently, and I am a developer for a SWRP server, I am trying to code a custom shop NPC to purchase ships from. I know I can use the F4 menu for that but it isn't as cool. I have my base entity, but I am not sure how to make it appear like an NPC, open a purchasing menu when E is pressed on it, how to impliment payment, and the spawning of the ship its self, any help would be greatly appreciated.
[QUOTE=DarkAura;52831288]So I have just started Lua coding recently, and I am a developer for a SWRP server, I am trying to code a custom shop NPC to purchase ships from. I know I can use the F4 menu for that but it isn't as cool. I have my base entity, but I am not sure how to make it appear like an NPC, open a purchasing menu when E is pressed on it, how to impliment payment, and the spawning of the ship its self, any help would be greatly appreciated.[/QUOTE]
Regarding the use of the Entity, you can use this function [url]https://wiki.garrysmod.com/page/Entity/Use[/url].
Then make a VGUI element appear on use, or whatever else you are using for the shop :)
As for the payment, assuming you're using DarkRP as a base, it provides the following meta functions:
[CODE]
ply:getDarkRPVar("money") //will return how much money ply has
ply:addMoney(50) //will add $50 to the player's balance
ply:addMoney(-100) //will take $100 from the player's balance, same as the one above but note use of a negative value to take vs. add
ply:canAfford(600) //will return true if the player can afford $600, false if not
[/CODE]
Use these to interact with the player's balance.
As <Entity>:Use is only available serverside, you will have to tell the player to open a menu from the server, using net messages.
[url]https://wiki.garrysmod.com/page/Net_Library_Usage[/url]
Quick example:
[CODE]
//Serverside:
util.AddNetworkString("OpenMenu") //doesnt matter what you put in here, just has to be a string and unique
function ENT:Use(a, caller) //our use function, caller is the player who used the entity
if(IsValid(caller) && caller:IsPlayer()) then //making sure a player actually used this
net.Start("OpenMenu") //starting our net message which we cached above
net.Send(caller) //send it to the player who used the entity
end
end
//Clientside:
net.Receive("OpenMenu", function() //hook up a function to be called when we receive the OpenMenu net message (which we precached and later sent @ use function on the serverside)
local frame = vgui.Create("DFrame") //create our frame
frame:SetSize(ScrW()/2, ScrH()/2) //set the size to half the width and half the height
frame:Center() //center it
frame:MakePopup() //make the cursor pop up and disable mouse and keyboard input to the game
end)
[/CODE]
To make purchasing work, you'd have to send a net message from the client to the server (e.g. on a vgui button click event) with what you want to buy. The server should then make sure the player can afford it (use the meta functions I showed above), then it should take the money and give the player the item.
There should be [URL="https://wiki.garrysmod.com/page/Category:DScrollPanel"]DScrollPanel[/URL] to list items in shop menu.
[CODE]
net.Receive("OpenMenu", function() //hook up a function to be called when we receive the OpenMenu net message (which we precached and later sent @ use function on the serverside)
local frame = vgui.Create("DFrame") //create our frame
frame:SetSize(ScrW()/2, ScrH()/2) //set the size to half the width and half the height
frame:Center() //center it
frame:MakePopup() //make the cursor pop up and disable mouse and keyboard input to the game
local rgb = Color
local DScrollPanel = vgui.Create( "DScrollPanel", frame ) -- creating dscrollpanel child of frame
DScrollPanel:SetSize( 565, 300 ) -- sizing
DScrollPanel:SetPos( 5, 500 ) -- pos
DScrollPanel:Dock( FILL ) -- dscrollpanel will fill the frame.
DScrollPanel:Center() -- centering it.
local sbar = DScrollPanel:GetVBar() -- styling dscrollpanel's scrollbar. I recommend this.
function sbar:Paint( w, h )
draw.RoundedBox( 0, 0, 0, w, h, rgb(52, 73, 94,100) )
end
function sbar.btnUp:Paint( w, h )
draw.RoundedBox( 0, 0, 0, w, h, rgb(192, 57, 43) )
end
function sbar.btnDown:Paint( w, h )
draw.RoundedBox( 0, 0, 0, w, h, rgb(192, 57, 43) )
end
function sbar.btnGrip:Paint( w, h )
draw.RoundedBox( 0, 0, 0, w, h, rgb(44, 62, 80) )
end
end)
[/CODE]
Next thing there should be is a table.
Table for your items, their prices.
[CODE]
local itemtable = {} --main table
itemtable["entity_example"] = { -- registering a entity to table
name = "Health Pack",
entity = "health_pack",
price = 500,
}
[/CODE]
You have to send the table to client when opening the menu. [CODE]net.WriteTable(itemtable)[/CODE]
Now, you are ready to list the table. Go back to dscrollpanel end.
[CODE]
for k,v in pairs(itemtable) do
-- in this part you should make button to buy, model to show the entity maybe. It's up to you.
end
[/CODE]
Thanks for all the help guys! I hope all of this works well, either way thanks for taking your time to help me!
Sorry, you need to Log In to post a reply to this thread.