[LUA] Can't make my script autorun

Hello, sorry to bother anyone but for some reasons my script is not running as soon as I start a game.
It’s supposed to be the start of a script but it’s a pain to run it manually everytime and it’s not working properly in autorun.
Here’s the script:


concommand.Add("crunch", crunch)

function crunch(ply, cmd, args)

    local target

    for k,v in pairs(player.GetAll()) do

         if(string.find(string.lower(v:GetName()), string.lower(args[1]))) then

              target = v
              break

         end
    end

    if(IsEntity(target)) then



    else

         if(SERVER) then return end

         print("No player found going by the name of: '"..args[1].."'")

    end

end

Here’s the path I placed it in:


C:\Program Files (x86)\Steam\steamapps\common\GarrysMod\garrysmod\addons\Crunch\lua\autorun\shared.lua

I’d first start out by changing the name of the file.
Shared.lua is a common name, and Lua files get mounted to the same directory (essentially meaning they could overwrite eachother and one might not run with the same name).

Secondly, the formatting you should probably do something like this:



concommand.Add("crunch", function( _Player, _CMD, _Args )
    if !_Args || !_Args[1] then return end -- If no arguments supplied. We don't want errors!
    local target

    for k, v in pairs(player.GetAll()) do
        if (string.find(string.lower(v:GetName()), string.lower(_Args[1]))) then
            target = v
            break
        end
    end

    if (target:IsValid()) then
        --This code will execute. But there's nothing here?
    else
        if (SERVER) then return end
        print("No player found going by the name of: '" .. _Args[1] .. "'")
    end
end)


Just making sure you know this will only run if you type that command, right?

Whenever I try to run the command it still says there’s no crunch command :frowning:

Any errors? Add a print to the top of the file to see if it’s ran.

crunch is nil when you add the concommand. Put the concommand.Add part after you declare the function.