• is there any way to get this to work?
    22 replies, posted
I'm making a menu for a gamemode and when I click on a spawn icon, it gives me a weapon and takes my money. I'm using a function to give the player a weapon but I dont know what to do. is there any way I can fix this: the menu: [lua]include( 'menu/cl_shop' ) function menu() local DermaPanel = vgui.Create( "DFrame" ) DermaPanel:SetPos( 0, 0 ) DermaPanel:SetSize( 500, 500 ) DermaPanel:SetTitle( "Menu" ) DermaPanel:SetVisible( true ) DermaPanel:SetDraggable( false ) DermaPanel:ShowCloseButton( true ) DermaPanel:MakePopup() local PropertySheet = vgui.Create( "DPropertySheet" ) PropertySheet:SetParent( DermaPanel ) PropertySheet:SetPos( 0, 25 ) PropertySheet:SetSize( 500, 450 ) local panel = vgui.Create( "DPanel" ) local SheetItemOne = vgui.Create( "DButton", panel ) SheetItemOne:SetSize( 100, 50 ) SheetItemOne:SetPos( 20, 50 ) SheetItemOne:SetParent( panel ) local model = vgui.Create( "DModelPanel", panel ) model:SetModel( LocalPlayer():GetModel() ) model:SetSize( 300, 300 ) model:SetCamPos( Vector( 50, 50, 120 ) ) model:SetLookAt( Vector( 0, 0, 0 ) ) model:SetPos( 175, 100 ) model:SetParent( panel ) model:SetAnimated(ACT_WALK) model:SetAnimSpeed(1) local panel2 = vgui.Create( "DPanel" ) local SpawnIcon1 = vgui.Create('SpawnIcon') SpawnIcon1:SetPos(230, 35) SpawnIcon1:SetParent( panel2 ) SpawnIcon1:SetModel( "models/weapons/w_pist_p228" ) local SpawnIcon2 = vgui.Create('SpawnIcon') SpawnIcon2:SetPos(160, 35) SpawnIcon2:SetParent( panel2 ) SpawnIcon2:SetModel( "models/weapons/w_rif_ak47" ) local SpawnIcon3 = vgui.Create('SpawnIcon') SpawnIcon3:SetPos(90, 35) SpawnIcon3:SetParent( panel2 ) SpawnIcon3:SetModel( "models/weapons/w_pist_usp" ) local SpawnIcon4 = vgui.Create('SpawnIcon') SpawnIcon4:SetPos(20, 35) SpawnIcon4:SetParent( panel2 ) SpawnIcon4:SetModel( "models/weapons/w_pist_glock18" ) SpawnIcon4.OnMousePressed = function() glock() return false end money = vgui.Create("DLabel", panel2) money:SetPos( 400, 25 ) money:SetText("You have $" .. pl:GetMoney()) money:SetParent( panel2 ) money:SizeToContents() PropertySheet:AddSheet( "Level", panel, "gui/silkicons/user", false, false, "Player stats, level, ect." ) PropertySheet:AddSheet( "Shop", panel2, "gui/silkicons/bomb", false, false, "you can buy stuff here!" ) end concommand.Add( "menu", menu )[/lua] the "shop" for all the functions: [lua]function glock( ply ) if ply:GetMoney() >= 200 then ply:Give( "weapon_glock18" ) ply:TakeMoney( 200 ) else ply:ChatPrint( "You don't have enough money!" ) end end[/lua] these are all clientside and I have the include in the menu.lua
You can't give players stuff clientside. [lua] SpawnIcon4.OnMousePressed = function() RunConsoleCommand("buystuff", "weapon_glock18") return false end[/lua] put this in a serverside file: [lua] concommand.Add("buystuff", function(p,c,a) p:Give(a[1]) end [/lua] That's pretty exploitable though, if you have enabled the console. I recommend doing that money/buy stuff serverside. Something like: [lua] local weapons={ ["weapon_glock18"]={ name="glock", cost=200, }, ["weapon_xxx"]={ name="*nextweapon*", cost=..., }, } concommand.Add("buytuff", function(p,c,a) if weapons[a[1]] and p.Money>=weapons[a[1]].cost then p:Give(a[1]) p.Money=p.Money-weapons[a[1]].cost end end [/lua]
[QUOTE=WeltEnSTurm;23312582]You can't give players stuff clientside. SpawnIcon4.OnMousePressed = function() RunConsoleCommand("buystuff", "weapon_glock18") return false end put this in a serverside file: concommand.Add("buystuff", function(p,c,a) p:Give(a[1]) end That's pretty exploitable though, if you have enabled the console. I recommend doing that money/buy stuff serverside. Something like: local weapons={ ["weapon_glock18"]={ name="glock", cost=200, }, ["weapon_xxx"]={ name="*nextweapon*", cost=..., }, } concommand.Add("buytuff", function(p,c,a) if weapons[a[1]] and p.Money>=weapons[a[1]].cost then p:Give(a[1]) p.Money=p.Money-weapons[a[1]].cost end end [/QUOTE] could you please explain this a little more, I'm a little new to lua, I've only been using it for a little over 2 months
[QUOTE=Dave_Parker;23313359]That was horribly newbie unfriendly. On the client, you will want to make the client run a console command when they press the button. SpawnIcon4.OnMousePressed = function() RunConsoleCommand("game_buyweapon", "glock") return false end Then on the server, you can have a table containing all the weapons and a function which handles the console command.local MyGamemodesWeapons = { ["glock"] = { "Price" = 200, "class" = "weapon_glock" }, ["m4a1"] = { "Price" = 500, "class" = "weapon_m4a1" } } -- And the function local function BuyWeapon(player, command, arguments) local weaponname = arguments[1] local weapontable = MyGamemodesWeapons[weaponname] if(!weapontable) then return end if(player.Money < weapontable.Price) then return end -- Player does not have enough money, don't do anything (or notify them if you like) player.Money = player.Money - weapontable.Price player:Give(weapontable.class) end concommand.Add("game_buyweapon", BuyWeapon)[/QUOTE] thanks, I knew the console command when you press the button thing, it was the table that scared me.
[lua] stuff = {} stuff[1] = { model = "Models/props_c17/furniturewashingmachine001a.mdl", desc = "Washer", command = "command_1" } stuff[2] = { model = "models/props_junk/garbage_milkcarton002a.mdl", desc = "Milk", command = "command_2" } local frame = vgui.Create("DFrame") local IconList = vgui.Create( "DPanelList", frame ) frame:Center() frame:SetSize(220,200) frame:SetTitle("Fridge") frame:MakePopup() IconList:EnableVerticalScrollbar( true ) IconList:EnableHorizontal( true ) IconList:SetPadding( 4 ) IconList:SetPos(10,30) IconList:SetSize(200, 160) for k, v in pairs(stuff) do icon = vgui.Create( "SpawnIcon", IconList ) icon:SetModel( v.model ) IconList:AddItem( icon ) icon:SetToolTip(v.desc) -- add desc = "" in the table. icon.DoClick = function() surface.PlaySound( "ui/buttonclickrelease.wav" ) LocalPlayer():ConCommand("gm_spawn", v.model) end end [/lua]
dave_parker's didn't work, is there an advanced table guide?
It's not working, it takes- I mean [I]steals[/I] my money, and I don't get a gun: [lua]function p228( ply ) if ply:GetMoney() >= 150 then ply:Give( "weapon_mad_p228" ) ply:TakeMoney( 150 ) else ply:ChatPrint( "You don't have enough money!" ) end end concommand.Add( "buyglock", glock ) concommand.Add( "buyak47", ak47 ) concommand.Add( "buyusp", usp ) concommand.Add( "buyp228", p228 )[/lua] I used separate concommand.Adds because I don't understand the tables [editline]09:16PM[/editline] when I say /money it says I have nothing left (after clicking it multiple times)
The only issue I can see right now is that you might be trying to buy stuff while noclipping. That does not give you the weapon as far as I know. The code you posted seems fine to me.
Did you put this in a serverside file? Also, there should be some sort of error printed to the console. [lua] --I'll try to explain that table stuff to you. --This is a pretty simple table: local weapons={ "weapon_glock18", "weapon_ak47", "weapon_usp", "weapon_p228", } --Now, every entry of this table has an index. In this case, we didn't choose one, so gmod did it automatically. --glock18 has 1, ak47 has 2 and so on. To access a single entry of the table, we can use brackets. --For instance, print(weapons[1]) would print "weapon_glock18", print(weapons[3]) woudl print "weapon_usp". --if we do that ourselfes: local weapons={ [1]="weapon_glock18", [2]="weapon_ak47", [3]="weapon_usp", [4]="weapon_p228", } --We can also use strings as index. If you would make the weapon name the index, and the actual value the price it would look like this: local weapons={ ["weapon_glock18"]=150, ["weapon_ak47"]=300, ["weapon_usp"]=170, ["weapon_p228"]=150, } --If you now do print(weapons["weapon_glock18"]), it would print 150 and print(weapons["glock_ak47"]) would print 300. --Now you can put more tables into a single table, like this: local weapons={ { name="Glock 18", class="weapon_glock18", price=150, }, { name="AK 47", class="weapon_ak47", price=300, }, { name="USP", class="weapon_usp", price=170, }, { name="P228", class="weapon_p228", price=150, }, } --Now, weapons[1] would give you pretty much the whole first table, which would be: { name="Glock 18", class="weapon_glock18", price=150, }, --print(weapons[1].name) would print "Glock 18", print(weapons[2].price) would print 300 and so on. --to make it easier, you can do this: local weapon=weapons[1] print(weapon.price) print(weapon.name) --and so on. --Now, if you combine this with the index stuff I explained, it would look like: local weapons={ ["weapon_glock18"]={ name="Glock 18", price=150, }, ["weapon_ak47"]={ name="AK 47", price=300, }, ["weapon_usp"]={ name="USP", price=170, }, ["weapon_p228"]={ name="P228", price=150, }, } local weapon=weapons["weapon_glock18"] print(weapon.name) -- prints "Glock 18" print(weapon.price) -- prints 150 print(weapons["weapon_ak47"].price) -- prints 300 [/lua] This is pretty much my first ("real") tutorial thing, I hope it helps.
[QUOTE=WeltEnSTurm;23324735]Did you put this in a serverside file? Also, there should be some sort of error printed to the console. --I'll try to explain that table stuff to you. --This is a pretty simple table: codeThis is pretty much my first ("real") tutorial thing, I hope it helps.[/QUOTE] thanks, this helped me learn a lot about tables, so I will use this. I made a function to give me money and I used a chat command to call it and when I used it, it worked (this was before I used the table btw). [editline]05:00PM[/editline] all the tutorials I've seen on the wiki are all for real beginners and I think you did a really good job of explaining this to me.
could you explain the function( p, c, a ), like what those arguments are because it's broken here's the code: [lua]local weapons = { ["weapon_glock18"]={ name = "Glock 18", price = 250, }, ["weapon_ak47"]={ name = "Kalashnikov", price = 550, }, ["weapon_usp"]={ name = "USP .45", price = 300, }, ["weapon_m4a1"]={ name = "M4A1", price = 600, }, ["weapon_physcannon"]={ name = "Zero-Point Energy Field Manipulator", price = 600, }, } function shop(p,c,v) if weapons[a[1]] and p.Money>=weapons[a[1]].cost then p:Give(a[1]) p.SetMoney=p.GetMoney-weapons[a[1]].cost end end concommand.Add("buy", shop )[/lua]
[QUOTE=Carmine3777;23347040]could you explain the function( p, c, a ), like what those arguments are because it's broken here's the code: [lua]local weapons = { ["weapon_glock18"]={ name = "Glock 18", price = 250, }, ["weapon_ak47"]={ name = "Kalashnikov", price = 550, }, ["weapon_usp"]={ name = "USP .45", price = 300, }, ["weapon_m4a1"]={ name = "M4A1", price = 600, }, ["weapon_physcannon"]={ name = "Zero-Point Energy Field Manipulator", price = 600, }, } function shop(p,c,v) if weapons[a[1]] and p.Money>=weapons[a[1]].cost then p:Give(a[1]) p.SetMoney=p.GetMoney-weapons[a[1]].cost end end concommand.Add("buy", shop )[/lua][/QUOTE] I think it's short for Player, Command, Variable but Im not sure.
[QUOTE=Busymonkey;23347372]I think it's short for Player, Command, Variable but Im not sure.[/QUOTE] that's what I was thinking but I need an answer because it's not working. I meant to change the v back to an a which is probably argument. I used the code he gave me and used the concommand.Add from his first post and turned it into a function because it didn't work. [editline]02:11AM[/editline] would changing all the a's to weapons work? like: [lua]if [weapons[1]] and p.Money>=[weapons[1]].cost then[/lua] or: [lua]if weapons(weapons[1]) and p.Money>=weapons(weapons[1]).cost then[/lua] [editline]03:45AM[/editline] someone please help, I hate it when I cant solve problems with my gamemode.
change p,c,v to p,c,a you are using a in the function, but it's undefined since you changed it to v in the argument list.
it's giving me this error: gamemodes\carbsbuild\gamemode\menu\shop.lua:26: unexpected symbol near '[' [lua]function shop(p,c,a) if weapons[a[1]] and p.GetMoney>=weapons[a[1]].cost then p:Give([a[1]]) //line 26 p.SetMoney=p.TakeMoney(weapons[a[1]].cost) end end concommand.Add("buy", shop )[/lua]
someone help, I could already be finished with this.
You are giving the player [a[1]]. Change it to a[1].
it gives me this error: gamemodes\carbsbuild\gamemode\menu\shop.lua:25: attempt to compare nil with function [editline]10:25PM[/editline] ok, I changed it and got an error: attempt to compare nil with number on line 25 [lua]function shop(ply,c,a) if weapons[a[1]] and ply:GetMoney()>=weapons[a[1]].cost then //line 25 ply:Give(a[1]) ply:TakeMoney(weapons[a[1]].cost) end end[/lua]
There is no weapons[a[1]].cost value, only .price.
Well I got lost in all these posts but I will try to help you out from the start. What you want to do to purchase guns from a menu is this: Send a message to the server when you click the buy button. Make your checks to see if the player has sufficient funds and such. Give the player his weapon and take away his cash. Clientside: [lua] local WeaponsList = vgui.Create("DPanelList", DermaPanel) WeaponsListt:SetPos(0, 0) WeaponsList:SetSize(200, 200) WeaponsList:SetSpacing(4) WeaponsList:EnableHorizontal(true) WeaponsList:EnableVerticalScrollbar(true) for k, v in pairs(Weapons) do local SpawnIcon = vgui.Create('SpawnIcon') SpawnIcon:SetModel(v.Model) SpawnIcon:SetToolTip("Cost: $"v.Cost) SpawnIcon.OnMousePressed = function() RunConsoleCommand("PurchaseWeapon", k) end WeaponsList:AddItem(SpawnIcon) end [/lua] Serverside: [lua] function PurchaseWeapon(pl, cmd, arg) --Player, Command, Arguments if !arg[1] then return end --If there is no arguments end the function now. if !Weapons[arg[1]] then return end --Since the argument we are recieving from the client should be something like 'weapon_glock18', we should have a table in the 'Weapons' table with that name, if not then end the function as we have no info on what we are buying. if pl:GetMoney() > Weapons[arg[1]].Cost then pl:TakeMoney(Weapons[arg[1]].Cost) --Take away cash. pl:Give(Weapons[arg[1]]) --Give weapon. else pl:ChatPrint("You do not have enough money for this weapon!") --Send message to players chat. end end concommand.Add("PurchaseWeapon", PurchaseWeapon) [/lua] Shared: [lua] --A good place to keep a table that is use for info only is shared, this way the server and client can access it. Weapons = {} Weapons["weapon_glock18"] = { Model = "models/weapons/w_pist_p228" Name = "Glock", Cost = 250 } [/lua] That should be more than enough to get you going, you just need to piece it all together and try to understand what exactly is going on with it all.
[QUOTE=WeltEnSTurm;23400393]There is no weapons[a[1]].cost value, only .price.[/QUOTE] thanks, it worked and thank you find me for helping me learn a bit.
it says something about line 610 in commands from the default sandbox gamemode being a nil value for all sweps I try to spawn, it works in sandbox but not my gamemode. [editline]08:58PM[/editline] the nil value is weapons.GetStored [editline]09:27PM[/editline] never mind, I fixed it, the table was called weapons.
Sorry, you need to Log In to post a reply to this thread.