Simple Lua Help(Concommands+Basic(VERY) .lua script placement)

Hey all, I’ve decided to slowly but surely work my way into trying to learn LUA or more importantly GLUA. First things first, i’ve run a snag. I attempted to code along with online sources and others(CGHIPPO + FACEPUNCH THREADS)



AddCSLuaFile()

concommand.Add( "b", function( ply, cmd, argms )
	ply:GodEnable()
	print(" Godmode : ON")
if ply:HasGodMode() then ply:GodDisable()
 print(" Godmode : OFF")
end
end)

hook.Add( "PlayerSay", "Chatcmd", function(ply, text, pblc) 
    if string.lower(text) == "!b" then
        concommand.Run(ply,"b")
    end
end)


But i get no errors in console about the code so i am thinking the code doesn’t load so my next issue is whats the basic .lua placement for a file like this?

Do i do autorun/godmode.lua??

Or do i do addons/godmode/lua/shared.lua?

Second option, about making a folder in addons.

Also, your command won’t work, regardless. It will enable godmode and then immediately disable it.

Use this



concommand.Add( "b", function( ply, cmd, argms )
	if ply:HasGodMode() then
		ply:GodDisable()
		print(" Godmode: OFF")
	else
		ply:GodEnable()
		print(" Godmode: ON")
	end
end)


Ah okay! That makes sense rather than having “skips” in the coding when it should all really be strung together. Let me try it out and see if it works!

Unrelated note: How come for GodDisable and GodEnable you don’t have to say If SERVER then??

[editline]26th September 2015[/editline]

Interesting. I don’t think i have godmode setup correctly. I get this error?




[ERROR] lua/autorun/godmode.lua:2: attempt to call method 'GodEnable' (a nil value)
  1. unknown - lua/autorun/godmode.lua:2
   2. unknown - lua/includes/modules/concommand.lua:54


You need if SERVER then

Im slowly working through it and i still get the error using this.



if SERVER then
concommand.Add( "b", function( ply, cmd, argms )
	if ply:HasGodMode() then
		ply:GodDisable()
		print(" Godmode: OFF")
	else
		ply:GodEnable()
		print(" Godmode: ON")
	end
end)
end


Do i need to runconsolecommand on client for it to not get that same error?

The concommand you want has to be run serverside. You can do that two ways.

Either having the single lua file being in lua/autorun, with the template



if SERVER then
    AddCSLuaFile()
    --stuff here
end


Or, since it’s only a server-side command, you can just place the file in lua/autorun/server so that you don’t need to include the stuff above.

Both ways would allow the client to use the console command.

You only want to have the con command clientside if you’re running an operation clientside or want to get info that’s only clientsided.

Hey DeathWard, I put this code below into both addons/godmode/lua/autorun/server and into lua/server/autorun/server and it says unknown command g when i type g into console and nothing happens when i type “!g” so i think it isn’t even being loaded.



AddCSLuaFile()
ply = LocalPlayer()
concommand.Add( "g", function( ply, cmd, argms )

	if ply:HasGodMode() then
		ply:GodDisable()
		print("Godmode: OFF")
	else
		ply:GodEnable()
		print("Godmode: ON")
end
end)


hook.Add( "PlayerSay", "Chatcmd", function(ply, text, pblc) 
    if string.lower(text) == "!g" then
        concommand.Run(ply,"g")
    end
end)


I am having problems with GodEnable and GodDisable also. GodEnabled has no error but just doesn’t work, and GodDisable produces an error. Will update with the info.

[editline]27th September 2015[/editline]

Okay, this is what I have.


TEAM_ADMIN = DarkRP.createJob("Administration", {
    color = Color(41, 41, 41, 255),
    model = "models/player/mnu_soldier.mdl",
    description = [[You are administration, you cannot RP, you must help people with administrative situations.]],
    weapons = {},
    command = "admin",
    max = 10,
    salary = 0,
    admin = 0,
    vote = false,
    hasLicense = false,
	PlayerSpawn = function(ply) ply:GodEnable() end,
	PlayerDeath = function(ply) ply:GodDisable() end,
	OnPlayerChangedTeam = function(ply) ply:GodDisable() end,
    customCheck = function(ply) return
    table.HasValue({"moderator", "owner", "superadmin", "admin"}, ply:GetNWString("usergroup")) or ply:IsAdmin() end,
    CustomCheckFailMsg = "You must be a staff member.",
})

It gives me this. The GodEnable works.


[ERROR] addons/darkrpmodification/lua/darkrp_customthings/jobs.lua:147: attempt to call method 'GodDisable' (a nil value)
  1. OnPlayerChangedTeam - addons/darkrpmodification/lua/darkrp_customthings/jobs.lua:147
   2. Call - gamemodes/darkrp/gamemode/modules/base/sh_gamemode_functions.lua:18
    3. Function - gamemodes/darkrp/gamemode/modules/base/cl_gamemode_functions.lua:62
     4. unknown - lua/includes/modules/usermessage.lua:87

[editline]27th September 2015[/editline]

Nevermind, the GodEnable doesn’t work. I had sv_godmode 1

Ahhh I wonder whats wrong :confused:

Edit : I think for now i’ll just set player health to something incredibly high like 1 billion or something. JK. Anything over 6 figures crashes the client

You don’t need to define ply as a localplayer since the script is running on the server.

Same goes with AddCSLuaFile.

This works just fine, when placed in lua/autorun/server.



local function ToggleGodMode(ply)
	if ply:HasGodMode() then
		ply:GodDisable()
		print("Godmode: OFF")
	else
		ply:GodEnable()
		print("Godmode: ON")
	end
end


concommand.Add("g", ToggleGodMode)




hook.Add( "PlayerSay", "Chatcmd", function(ply, text, pblc) 
	if string.lower(text) == "!g" then
		ToggleGodMode(ply)
	end
end)


There’s no need for the function to be that big for something so simple, you could just as easily to it like this:

[lua]
local function ToggleGodMode( ply )
print( string.format( “Godmode: %s”, ply:HasGodMode() and “OFF” or “ON” ) )
return ply:HasGodMode() and ply:GodDisable() or ply:GodEnable()
end
[/lua]

Hey James and Deathward thanks for all the help! Sorry to everyone those as this felt like a spoonfeeding :confused:

[editline]27th September 2015[/editline]

Using this code i get this same error



[ERROR] lua/autorun/godmode.lua:2: attempt to call method 'GodEnable' (a nil value)
  1. unknown - lua/autorun/godmode.lua:2
   2. unknown - lua/includes/modules/concommand.lua:54


You are running the code from the client.
Stop running it from the client.

Delete your file from the root directory of GMod inside of lua/autorun/godmode.lua

Make a new folder inside your addons folder.
Structure it as thus:



addons -- base addon folder
∟ godmode
    ∟ lua
        ∟ autorun
            ∟ server
               ∟ godmode.lua


YAYA it works now! Except for when i type !g in chat it doesn’t enable/disable godmode.