• ply:give not working for some reason
    55 replies, posted
I'm kinda noob at lua so take that in mind [CODE] ----------------------------------------------------- -------------------------------------------- NPC.name = "Imperial Officer" function NPC:onStart() local ply = LocalPlayer() self:addText("Hi, what do you need?") self:addOption("How do I get trained?", function() self:addText("Ask someone to train you by typing /comms I need trainer.") self:addLeave("Thanks!") end) self:addOption("How do I donate?", function() self:addText("Type !donate or !shop and you will see all information there. Be sure to read our TOS!") self:addLeave("Thanks!") end) self:addOption("How do I become one of the High Inquisitors or how do I become Commander?", function() self:addText("Type !website and look at our applications, there you can apply for them.") self:addLeave("Thanks!") end) self:addOption("I see a lot of errors, how do I fix that?", function() self:addText("Type !content in chat and subscribe to our content.") self:addLeave("Thanks!") end) self:addOption("I have a suggestion, where do I post it?", function() self:addText("Type !suggestions in chat and post a suggestion or post it on our forums by typing !website.") self:addLeave("Thanks!") end) self:addOption("I need a training weapon", function() local ply:give( "tfa_e11_training" ) self:addText("Here you go.") self:addLeave("Thanks!") end) end[/CODE] This is the code and I get this error, can you help? error : [CODE] [ERROR] addons/cn_npc/lua/cn_npcs/test/cl_init.lua:38: attempt to call method 'give' (a nil value) 1. callback - addons/cn_npc/lua/cn_npcs/test/cl_init.lua:38 2. callback - addons/cn_npc/lua/cn_npc_system/cl_quest.lua:89 3. unknown - addons/cn_npc/lua/cn_npc_system/cl_quest.lua:165[/CODE]
Lua is case-sensitive.
Oh okey, will try :) Thank for quick reply, I will tell you if it works. [editline]3rd July 2017[/editline] by server side what do you mean? What kind of method? [editline]3rd July 2017[/editline] btw this is the new error [ERROR] lua/vgui/dframe.lua:79: bad argument #1 to 'SetText' (string expected, got nil) 1. SetText - [C]:-1 2. SetTitle - lua/vgui/dframe.lua:79 3. setup - addons/cn_npc/lua/cn_npc_system/cl_quest.lua:135 4. func - addons/cn_npc/lua/cn_npc_system/cl_cn_npc.lua:35 5. unknown - lua/includes/extensions/net.lua:32
Can you give us dframe.lua, specifically line 79 and the area around it? Otherwise we can't help. [editline]3rd July 2017[/editline] It's in the lua/vgui folder as far as I can tell. [editline]3rd July 2017[/editline] [QUOTE=martinkordi;52428602]Oh okey, will try :) Thank for quick reply, I will tell you if it works. [editline]3rd July 2017[/editline] by server side what do you mean? What kind of method? [editline]3rd July 2017[/editline] btw this is the new error [ERROR] lua/vgui/dframe.lua:79: bad argument #1 to 'SetText' (string expected, got nil) 1. SetText - [C]:-1 2. SetTitle - lua/vgui/dframe.lua:79 3. setup - addons/cn_npc/lua/cn_npc_system/cl_quest.lua:135 4. func - addons/cn_npc/lua/cn_npc_system/cl_cn_npc.lua:35 5. unknown - lua/includes/extensions/net.lua:32[/QUOTE] Server side meaning that it will only work in a script that is run on the server. The only ones not run on the server are in a folder called "client" (I think) and if you have never seen that, then all of your scripts run on the server so don't worry.
[QUOTE=Lavacoal123;52429714]Can you give us dframe.lua, specifically line 79 and the area around it? Otherwise we can't help. [editline]3rd July 2017[/editline] It's in the lua/vgui folder as far as I can tell. [editline]3rd July 2017[/editline] Server side meaning that it will only work in a script that is run on the server. The only ones not run on the server are in a folder called "client" (I think) and if you have never seen that, then all of your scripts run on the server so don't worry.[/QUOTE] It's Derma and it uses LocalPlayer(), which means that this must be in a client file, not Lua. Also, what's with the "[B]local[/B] ply:give()"
Yeah, remove the local there as it's unnecessary. Also, show us the code around these two areas: 3. setup - addons/cn_npc/lua/cn_npc_system/cl_quest.lua:135 4. func - addons/cn_npc/lua/cn_npc_system/cl_cn_npc.lua:35
This is the line in dframe function PANEL:SetTitle( strTitle ) self.lblTitle:SetText( strTitle ) end [editline]4th July 2017[/editline] [QUOTE=monkeymacman;52429987]It's Derma and it uses LocalPlayer(), which means that this must be in a client file, not Lua. Also, what's with the "[B]local[/B] ply:give()"[/QUOTE] Shouks i remove local? [editline]4th July 2017[/editline] This ia line 35 cnPanels.quest:setup(uniqueID) if (cnQuests[uniqueID].onStart) then cnQuests[uniqueID]:onStart() end end end) [editline]4th July 2017[/editline] This is line 135 self:SetTitle(data.name) [editline]4th July 2017[/editline] All i want is when you press "i need training weapon" then you should get one
Where is strTitle defined? The error says its undefined. Yes, remove local. Make sure setup is Setup. It's probably case sensitive like a lot of other functions in LUA. Where is data.name defined? Is name supposed to be Name? [editline]4th July 2017[/editline] Telling us where UniqueID is defined can also help.
data.name is undefined.
Well I haven't really made that script, all I want is when I press I need training gun, I want it to give player a training gun [editline]4th July 2017[/editline] This is the error right now [ERROR] lua/vgui/dframe.lua:79: bad argument #1 to 'SetText' (string expected, got nil) 1. SetText - [C]:-1 2. SetTitle - lua/vgui/dframe.lua:79 3. setup - addons/cn_npc/lua/cn_npc_system/cl_quest.lua:135 4. func - addons/cn_npc/lua/cn_npc_system/cl_cn_npc.lua:35 5. unknown - lua/includes/extensions/net.lua:32 [editline]4th July 2017[/editline] actually that was my fault but this is current errorž [ERROR] addons/cn_npc/lua/cn_npcs/test/cl_init.lua:37: attempt to call method 'Give' (a nil value) 1. callback - addons/cn_npc/lua/cn_npcs/test/cl_init.lua:37 2. callback - addons/cn_npc/lua/cn_npc_system/cl_quest.lua:89 3. unknown - addons/cn_npc/lua/cn_npc_system/cl_quest.lua:165 [editline]4th July 2017[/editline] the same one as before, I did remove local tho.
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/Give]Player:Give[/url] is serverside only.
Make sure the function containing Player:Give() is in init.lua, or even shared.lua if you really have to. [editline]4th July 2017[/editline] Check Line 89 and 165 as well, I'm not great at reading errors but I believe you made the same mistake there too. Before including a function in a script check the GMod Wiki or maurits.tv to check if it is serverside, clientside, or shared.
so am I able to even use ply:give in this code? [QUOTE]----------------------------------------------------- -------------------------------------------- NPC.name = "Imperial Officer" function NPC:onStart() local ply = LocalPlayer() self:addText("Hi, what do you need?") self:addOption("How do I get trained?", function() self:addText("Ask someone to train you by typing /comms I need trainer.") self:addLeave("Thanks!") end) self:addOption("How do I donate?", function() self:addText("Type !donate or !shop and you will see all information there. Be sure to read our TOS!") self:addLeave("Thanks!") end) self:addOption("How do I become one of the High Inquisitors or how do I become Commander?", function() self:addText("Type !website and look at our applications, there you can apply for them.") self:addLeave("Thanks!") end) self:addOption("I see a lot of errors, how do I fix that?", function() self:addText("Type !content in chat and subscribe to our content.") self:addLeave("Thanks!") end) self:addOption("I have a suggestion, where do I post it?", function() self:addText("Type !suggestions in chat and post a suggestion or post it on our forums by typing !website.") self:addLeave("Thanks!") end) self:addOption("I need a training weapon", function() local ply:give( "tfa_e11_training" ) self:addText("Here you go.") self:addLeave("Thanks!") end) end[/QUOTE] [editline]4th July 2017[/editline] If so, could anyone guide me through the process? I know I'm pain in the ass, I just want to know how to do this in the future :)
No. That code is clientside as far as I can tell because you used LocalPlayer(), a clientside function. If this is a serverside script, then yes. I can help you most if you post it on GitHub, would be happy to help there. Any means of file sharing really, just GitHub preferably. You can see what I did because it gives you the option to look at certain revisions and see the exact changes made. I could also help clean up the code and fix up the spacing. [editline]4th July 2017[/editline] Oh, and again, capital G in give. [editline]4th July 2017[/editline] I'm not sure what you could substitute for LocalPlayer() though. I found a useful snippet that will calculate the nearest entity, though. I made some changes so it does players only. [CODE] function FindNearestPlayer( pos, range ) local nearestEnt; range = range ^ 2 for _, ent in pairs( player.GetAll() ) do local distance = (pos - ent:GetPos()):LengthSqr() if( distance <= range ) then nearestEnt = entity range = distance end end return nearestEnt; end [/CODE]
If you want people to get their gun via the derma, your best bet is to use the Net Library to network the button press to the server. [URL="https://wiki.garrysmod.com/page/Net_Library_Usage"]https://wiki.garrysmod.com/page/Net_Library_Usage[/URL] This should help you a lot in regards of how it works.
[QUOTE=QuackDuck;52432949]If you want people to get their gun via the derma, your best bet is to use the Net Library to network the button press to the server. [URL="https://wiki.garrysmod.com/page/Net_Library_Usage"]https://wiki.garrysmod.com/page/Net_Library_Usage[/URL] This should help you a lot in regards of how it works.[/QUOTE] Good idea. This snippet I made should help in that regard. [CODE] --Put this at the start of your code util.AddNetworkString("GPly") --Put this a line before you call the Player:Give() function net.Start( "GPly" ) net.WriteEntity( ply ) --Replace the Player:Give() function with this net.SendToServer() [/CODE] In the same folder as cl_init.lua, put this in init.lua [CODE] net.Receive( "GPly", function() local ply = net.ReadEntity() ply:Give("tfa_e11_training") end ) [/CODE] Boom! Should work, in theory. Can't check right now. And for the last time, get rid of the "local"! [editline]4th July 2017[/editline] One more thing, what are these functions? They remind me of a Derma Menu but I can't find some of these on the GMod wiki. The only function on the wiki (other than Player:Give() ) that I can find is this one: [URL="https://wiki.garrysmod.com/page/DMenu/AddOption"]DMenu:AddOption[/URL]
Oh Okey, gonna try that, thank you for all your help and sorry for wasting your time. Gonna try it now. [editline]5th July 2017[/editline] I will post results here, hope it works :) [editline]5th July 2017[/editline] So this is how the code looks now [QUOTE]util.AddNetworkString("GPly") ----------------------------------------------------- -------------------------------------------- NPC.name = "Imperial Officer" function NPC:onStart() self:addText("Hi, what do you need?") self:addOption("How do I get trained?", function() self:addText("Ask someone to train you by typing /comms I need trainer.") self:addLeave("Thanks!") end) self:addOption("How do I donate?", function() self:addText("Type !donate or !shop and you will see all information there. Be sure to read our TOS!") self:addLeave("Thanks!") end) self:addOption("How do I become one of the High Inquisitors or how do I become Commander?", function() self:addText("Type !website and look at our applications, there you can apply for them.") self:addLeave("Thanks!") end) self:addOption("I see a lot of errors, how do I fix that?", function() self:addText("Type !content in chat and subscribe to our content.") self:addLeave("Thanks!") end) self:addOption("I have a suggestion, where do I post it?", function() self:addText("Type !suggestions in chat and post a suggestion or post it on our forums by typing !website.") self:addLeave("Thanks!") end) net.Start( "GPly" ) net.WriteEntity( ply ) self:addOption("I need a training gun.", function() net.SendToServer() self:addText("Here you go.") self:addLeave("Thanks!") end) end[/QUOTE] this is the error it gives me now [ERROR] lua/vgui/dframe.lua:79: bad argument #1 to 'SetText' (string expected, got nil) 1. SetText - [C]:-1 2. SetTitle - lua/vgui/dframe.lua:79 3. setup - addons/cn_npc/lua/cn_npc_system/cl_quest.lua:135 4. func - addons/cn_npc/lua/cn_npc_system/cl_cn_npc.lua:35 5. unknown - lua/includes/extensions/net.lua:32 [editline]5th July 2017[/editline] And yeah, I did the init.lua aswell so that is done aswell.
[QUOTE=Lavacoal123;52433318]Good idea. This snippet I made should help in that regard.[/QUOTE] The snippet won't work. [QUOTE=Lavacoal123;52433318] [CODE] --Put this at the start of your code util.AddNetworkString("GPly") --Put this a line before you call the Player:Give() function net.Start( "GPly" ) net.WriteEntity( ply ) --Replace the Player:Give() function with this net.SendToServer() [/CODE] In the same folder as cl_init.lua, put this in init.lua [CODE] net.Receive( "GPly", function() local ply = net.ReadEntity() ply:Give("tfa_e11_training") end ) [/CODE] Boom! Should work, in theory. Can't check right now. And for the last time, get rid of the "local"! [/QUOTE] works, but bad. [QUOTE=Lavacoal123;52433318] [editline]4th July 2017[/editline] One more thing, what are these functions? They remind me of a Derma Menu but I can't find some of these on the GMod wiki. Custom functions, this is from chessnuts(?) gmodstore addon. The only function on the wiki (other than Player:Give() ) that I can find is this one: [URL="https://wiki.garrysmod.com/page/DMenu/AddOption"]DMenu:AddOption[/URL][/QUOTE] Custom functions, this is from chessnuts(?) gmodstore addon. Here: Proper indentation is very important, please use it next time. Client: [code] NPC.name = "Imperial Officer" function NPC:onStart() self:addText("Hi, what do you need?") self:addOption("How do I get trained?", function() self:addText("Ask someone to train you by typing /comms I need trainer.") self:addLeave("Thanks!") end) self:addOption("How do I donate?", function() self:addText("Type !donate or !shop and you will see all information there. Be sure to read our TOS!") self:addLeave("Thanks!") end) self:addOption("How do I become one of the High Inquisitors or how do I become Commander?", function() self:addText("Type !website and look at our applications, there you can apply for them.") self:addLeave("Thanks!") end) self:addOption("I see a lot of errors, how do I fix that?", function() self:addText("Type !content in chat and subscribe to our content.") self:addLeave("Thanks!") end) self:addOption("I have a suggestion, where do I post it?", function() self:addText("Type !suggestions in chat and post a suggestion or post it on our forums by typing !website.") self:addLeave("Thanks!") end) self:addOption("I need a training weapon", function() net.Start("give_wep") net.SendToServer() self:addText("Here you go.") self:addLeave("Thanks!") end) end [/code] Server: [code] util.AddNetworkString("give_wep") net.Receive("give_wep", function(_, ply) ply:Give("tfa_e11_training") end) [/code] People can exploit this, they can receive the gun from anywhere at anytime, I don't know how this addon does certain stuff but what you should do is: Create a table, insert NPC's into table once they get created, iterate over said table and check if the players distance is less than 250 or something.
Thank you! Will try it now! [editline]5th July 2017[/editline] this is the new error [QUOTE][ERROR] addons/cn_npc/lua/cn_npcs/test/cl_init.lua:32: Calling net.Start with unpooled message name! [[url]http://goo.gl/qcx0y][/url] 1. Start - [C]:-1 2. callback - addons/cn_npc/lua/cn_npcs/test/cl_init.lua:32 3. callback - addons/cn_npc/lua/cn_npc_system/cl_quest.lua:89 4. unknown - addons/cn_npc/lua/cn_npc_system/cl_quest.lua:165 [/QUOTE]
You forgot to include the serverside part.
I didn't. [editline]5th July 2017[/editline] I did put his code [QUOTE]util.AddNetworkString("give_wep") net.Receive("give_wep", function(_, ply) ply:Give("tfa_e11_training") end)[/QUOTE] in init.lua
Are you sure the file is being ran?
Well it should, its in the same folder as cl_init.lua [editline]5th July 2017[/editline] I'll copy exact codes from that file and from the cl_init [editline]5th July 2017[/editline] this is server [QUOTE]util.AddNetworkString("give_wep") net.Receive("give_wep", function(_, ply) ply:Give("tfa_e11_training") end)[/QUOTE] [editline]5th July 2017[/editline] client [QUOTE]NPC.name = "Imperial Officer" function NPC:onStart() self:addText("Hi, what do you need?") self:addOption("How do I get trained?", function() self:addText("Ask someone to train you by typing /comms I need trainer.") self:addLeave("Thanks!") end) self:addOption("How do I donate?", function() self:addText("Type !donate or !shop and you will see all information there. Be sure to read our TOS!") self:addLeave("Thanks!") end) self:addOption("How do I become one of the High Inquisitors or how do I become Commander?", function() self:addText("Type !website and look at our applications, there you can apply for them.") self:addLeave("Thanks!") end) self:addOption("I see a lot of errors, how do I fix that?", function() self:addText("Type !content in chat and subscribe to our content.") self:addLeave("Thanks!") end) self:addOption("I have a suggestion, where do I post it?", function() self:addText("Type !suggestions in chat and post a suggestion or post it on our forums by typing !website.") self:addLeave("Thanks!") end) self:addOption("I need a training weapon", function() net.Start("give_wep") net.SendToServer() self:addText("Here you go.") self:addLeave("Thanks!") end) end[/QUOTE]
To be fair I just found out about the Net Library, looks like it has some good potential though. Anyway, I think you need the [CODE] util.AddNetworkString("give_wep") [/CODE] in the beginning of the client script.
As you wish, gonna add that right now.
Can't find a definition for unpooled but I guess it's just fancy for undefined. [editline]5th July 2017[/editline] Hope it works.
Gonna inform you in a second if it works.
Well its not. debug with prints to see ifs actually ran.
Alright. One more note- if the AddNetworkString() function is called in both server and client, can it be put in shared.lua? Maybe I guess, but why add a sticker to a car?
I'm back at this error .... [QUOTE][ERROR] lua/vgui/dframe.lua:79: bad argument #1 to 'SetText' (string expected, got nil) 1. SetText - [C]:-1 2. SetTitle - lua/vgui/dframe.lua:79 3. setup - addons/cn_npc/lua/cn_npc_system/cl_quest.lua:135 4. func - addons/cn_npc/lua/cn_npc_system/cl_cn_npc.lua:35 5. unknown - lua/includes/extensions/net.lua:32 [/QUOTE]
Sorry, you need to Log In to post a reply to this thread.