• Help with my npc lua
    18 replies, posted
Ok guys, I suck at lua i'm a noob at it. I've tried my best of doing an lawyer, that unarrests you when you pay it. I know little lua i use templates to help me and try and gets some codes of other addons. I need to know how i can spawn it as a sent instead of just spawning it by position of map. I need to know if these scripts are correct, I will be happy if someone helps me out. I just want to learn from my mistakes, and I want it to work for my friend's darkrp gmod server. Clientside: [lua]local function myMenu() local DFrame1 = vgui.Create("DFrame") DFrame1:SetSize(413, 189) DFrame1:SetPos(105, 47) DFrame1:SetTitle("Lawyers4U") DFrame1:SetSizable(true) DFrame1:SetDeleteOnClose(false) DFrame1:MakePopup() local DButton2 = vgui.Create("DButton",DFrame1) DButton2:SetSize(121, 58) DButton2:SetPos(367, 137) DButton2:SetText('$2000 ARE YOU MAD!') DButton2.DoClick = function() end DButton1 = vgui.Create("DButton",DFrame1) DButton1:SetSize(113, 58) DButton1:SetPos(133, 138) DButton1:SetText('Get me out of here!') DButton1.DoClick = function( ply ) if (ply:CanAfford(2000) == true) then ply:Unarrest() end local DLabel1 = vgui.Create("DLabel",DFrame1) DLabel1:SetPos(111, 93) DLabel1:SetText(Hello i'm a lawyer, i can get you out of here in no time! But it will cost you $2000.) DLabel1:SizeToContents() end usermessage.Hook("ShopNPCUsed",myMenu) --Hook the menu, so we can use it Serverside[/lua] Serverside: [lua]function ENT:Initialize( ) --This function is run when the entity is created so it's a good place to setup our entity. self:SetModel( "models/humans/group01/female_01.mdl" ) -- Sets the model of the NPC. self:SetHullType( HULL_HUMAN ) -- Sets the hull type, used for movement calculations amongst other things. self:SetHullSizeNormal( ) self:SetNPCState( NPC_STATE_SCRIPT ) self:SetSolid( SOLID_BBOX ) -- This entity uses a solid bounding box for collisions. self:CapabilitiesAdd( CAP_ANIMATEDFACE | CAP_TURN_HEAD ) -- Adds what the NPC is allowed to do ( It cannot move in this case ). self:SetUseType( SIMPLE_USE ) -- Makes the ENT.Use hook only get called once at every use. self:DropToFloor() self:SetMaxYawSpeed( 90 ) --Sets the angle by which an NPC can rotate at once. end function ENT:AcceptInput( Name, Activator, Caller ) if Name == "Use" and Caller:IsPlayer() then umsg.Start("ShopNPCUsed", Caller) -- Prepare the usermessage to that same player to open the menu on his side. umsg.End() -- We don't need any content in the usermessage so we're sending it empty now. end end[/lua] Shared: [lua]ENT.Base = "base_ai" -- This entity is based on "base_ai" ENT.Type = "ai" -- What type of entity is it, in this case, it's an AI. ENT.AutomaticFrameAdvance = true -- This entity will animate itself. function ENT:SetAutomaticFrameAdvance( bUsingAnim ) -- This is called by the game to tell the entity if it should animate itself. self.AutomaticFrameAdvance = bUsingAnim end[/lua]
[lua ] [/ lua] tags please on your code.
Done
If you're going to use these clientside [lua]if (ply:CanAfford(2000) == true) then ply:Unarrest() end[/lua] i suggest a checking on the server side to make sure they have the correct money and can get arrested so [lua] function _R.Player:CanAfford(money) money = tonumber(money) if !self:GetNWInt("money",0) == money then self:ChatPrint("Sorry you cannot afford") return end end[/lua] [lua] function _R.Player:Unarrest(money) if self:CanAfford(money) then ply:ConCommand("Unarrestme") end end [/lua] [lua] DButton1.DoClick = function() LocalPlayer():Unarrest(2000) end [/lua] The server side you should beable to do by your self
Just for the record you put ENT:AcceptInput() twice.
Opps i will correct it looks like i pasted it twice. BTW how do i spawn my npc?
You need a SpawnFunction for it to show up in the Q-menu. Check Ricky's example SENT.
Ok eeeerm do i've to change the file names.
Here, Use this. also change the vectors to the position you want by typeing "getpos" in your console. [lua] hook.Add("InitPostEntity","your_entitys_folder_name",function() local angles = Angle(1.198000,-179.743011,0.000000) local npc = ents.Create("your_entitys_folder_name") npc:SetPos(pos) npc:SetAngles(angles) npc:Spawn() end)[/lua] Place this code in your init.lua folder
what if i don't have an init.lua i only have Clientside, Serverside and Shared
[QUOTE=bluemist;29332766]what if i don't have an init.lua i only have Clientside, Serverside and Shared[/QUOTE] If this is for a gamemode you need a init.lua make one
[lua] local DLabel1 = vgui.Create("DLabel",DFrame1) DLabel1:SetPos(111, 93) DLabel1:SetText(Hello i'm a lawyer, i can get you out of here in no time! But it will cost you $2000.) DLabel1:SizeToContents() [/lua] [lua] DLabel1:SetText(Hello i'm a lawyer, i can get you out of here in no time! But it will cost you $2000.) [/lua] [B]No Quotation Marks[/B]
So how am i going to make the init.lua?
File-new? [editline]22nd April 2011[/editline] Such a plethora of mistakes. When I am on my comp I will fox it.
I guess that this is too hard i can't do it ); Might aswell dump this
CL_INIT.LUA [lua] function myMenu() local DFrame1 = vgui.Create("DFrame") DFrame1:SetSize(413, 189) DFrame1:SetPos(105, 47) DFrame1:SetTitle("Lawyers4U") DFrame1:SetSizable(true) DFrame1:SetDeleteOnClose(false) DFrame1:MakePopup() local DButton2 = vgui.Create("DButton",DFrame1) DButton2:SetSize(121, 58) DButton2:SetPos(367, 137) DButton2:SetText('$2000 ARE YOU MAD!') DButton2.DoClick = function() --You need to close the menu here DFrame1:Close() end DButton1 = vgui.Create("DButton",DFrame1) DButton1:SetSize(113, 58) DButton1:SetPos(133, 138) DButton1:SetText('Get me out of here!') DButton1.DoClick = function() LocalPlayer():ConCommand("paylawyer") --You can't do all that on the server, you need to use a concommand end local DLabel1 = vgui.Create("DLabel",DFrame1) DLabel1:SetPos(111, 93) DLabel1:SetText("Hello i'm a lawyer, i can get you out of here in no time! But it will cost you $2000.") --This should be a string DLabel1:SizeToContents() end usermessage.Hook("ShopNPCUsed", myMenu) [/lua] INIT.LUA [lua] function ENT:Initialize( ) --This function is run when the entity is created so it's a good place to setup our entity. self:SetModel( "models/humans/group01/female_01.mdl" ) -- Sets the model of the NPC. self:SetHullType( HULL_HUMAN ) -- Sets the hull type, used for movement calculations amongst other things. self:SetHullSizeNormal( ) self:SetNPCState( NPC_STATE_SCRIPT ) self:SetSolid( SOLID_BBOX ) -- This entity uses a solid bounding box for collisions. self:CapabilitiesAdd( CAP_ANIMATEDFACE | CAP_TURN_HEAD ) -- Adds what the NPC is allowed to do ( It cannot move in this case ). self:SetUseType( SIMPLE_USE ) -- Makes the ENT.Use hook only get called once at every use. self:DropToFloor() self:SetMaxYawSpeed( 90 ) --Sets the angle by which an NPC can rotate at once. end function ENT:AcceptInput( Name, Activator, Caller ) if Name == "Use" and Caller:IsPlayer() then umsg.Start("ShopNPCUsed", Caller) -- Prepare the usermessage to that same player to open the menu on his side. umsg.End() -- We don't need any content in the usermessage so we're sending it empty now. end end --Now what I am adding concommand.Add("paylawyer", function(ply, cmd, args) if (ply:CanAfford(2000) == true) then ply:Unarrest() end --ATTENTION. YOU NEED TO ADD SOMETHING TO REMOVE YOUR MONEY. I AM NOT FAMILIAR WITH DARKRP SO I DO NOT KNOW WHAT IT IS. end) [/lua]
You have to define a string by surrounding it in quotes.
[lua] if ply:CanAfford(2000) then ply:AddMoney(-2000) ply:unarrest() else Notify(ply, 1, 4, "You cannot afford sorry.") end end end) [/lua] Can i add this? [lua]hook.Add("InitPostEntity","your_entitys_folder_name",function() local angles = Angle(1.198000,-179.743011,0.000000) local npc = ents.Create("your_entitys_folder_name") npc:SetPos(pos) npc:SetAngles(angles) npc:Spawn() end)[/lua] Do i add this in the bottom of the script and use getpos? I also want to add at least 5 of them in diffrent posistions or make them as an entity
PLZ Don't BUMP this thread ):
Sorry, you need to Log In to post a reply to this thread.