Simple addon not working

I’m not sure why this is working. I have followed quite a few tutorials to get the basics but I’m confused at how to make a simple addon


AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include("shared.lua")

function GM:PlayerInitialSpawn(ply)
	print("Initial Spawn is working")
end


function Kill()
	local ply = LocalPlayer()
	ply:Kill()
end

hook.Add("InVehicle","inVehicle",Kill)
	

Iirc, PLAYER:Kill() is serverside.

I put the Kill function into the init.lua file but it still doesnt work and still refuses to even print the message in PlayerInitialSpawn

I’d assume you mean gamemode, not an addon?
I’d assume the first code is init.lua, what is the second code about?

No its an addon, that’s what I don’t understand, I’ve looked through other addons and they have structure that have the init, shared and cl_init. The second code is shared.lua

  1. You’re overriding a GAMEMODE function. Use hook.Add instead.
  2. LocalPlayer is clientside, PLAYER:Kill is serverside.

I would really appreciate it if you could tell me what is wrong here. I don’t understand what is meant when a GAMEMODE function is overridden, so I tried to mess around with it.

Init


AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include("shared.lua")


function whenplayerspawn()
	print("Initial Spawn is working")
end

function Kill()
	Player:Kill()
end

Shared



function GM:Initialize()
	print("Hi!")
	hook.Add("PlayerInitialSpawn", "onPlayerSpawn", whenplayerspawn)
	hook.Add("InVehicle","inVehicle",Kill)
end


You are using code for a gamemode.

In what files do you put those codes? Full paths please.

C:\Program Files\Steam\SteamApps\common\GarrysMod\garrysmod\addons\myaddon

inside myaddon file is a file called lua and info.txt

inside lua file is init, shared and cl_init

Ok. Get rid of the info.txt, it is no longer needed.

Then move lua/shared.lua to lua/autorun/myaddon_shared.lua
lua/init.lua to lua/autorun/server/myaddon_init.lua
lua/cl_init.lua to lua/autorun/client/myaddon_cl_init.lua

Note that all files must be uniquely named so that they don’t override other files on other addons or the game itself.

Then your files must contain this: ( as example )
init.lua



AddCSLuaFile("autorun/client/myaddon_cl_init.lua")
AddCSLuaFile("autorun/myaddon_shared.lua")

-- files in autorun are ran automatically by the game

function whenplayerspawn( ply ) --PlayerInitialSpawn has one argument
	print( "Initial Spawn is working", ply )
end
hook.Add( "PlayerInitialSpawn", "myaddon_PlayerInitialSpawn", whenplayerspawn ) -- in addons you use hook.Add, GM:* stuff is for GAMEMODES

-- There's no such hook as "InVehicle". There's a hook called "PlayerEnteredVehicle" though
local function Kill( ply,veh, role ) -- Localize functions so they are not overridden by other addons.
	ply:Kill()
end
hook.Add( "PlayerEnteredVehicle", "myaddon_PlayerEnteredVehicle_whatever", Kill )

cl_init.lua



hook.Add("HUDPaint", "myaddon_myhookname_must be unique", function() -- You can define hooks like this too
	draw.RoundedBox( 0, 0, 0, 128, 128, Color( 0, 0, 0, 128 ) ) -- Draw a box
end )

shared.lua



if ( CLIENT ) then -- Run this code only on client, since you cannot draw server side
	hook.Add("HUDPaint", "myaddon_myotherhook", function()
		draw.RoundedBox( 0, 0, 128, 128, 128, Color( 128, 0, 0, 128 ) ) -- Draw another box
	end )
end

-- The CLIENT and SERVER global variables are only used when you need to separate code to be run specifically on a SERVER or on a CLIENT in a SHARED file.


Your problem is that you do not know how to code.