• Simple addon not working
    9 replies, posted
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 [CODE]AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") include("shared.lua") function GM:PlayerInitialSpawn(ply) print("Initial Spawn is working") end[/CODE] [CODE]function Kill() local ply = LocalPlayer() ply:Kill() end hook.Add("InVehicle","inVehicle",Kill) [/CODE]
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 [B]gamemode[/B], not an [B]addon[/B]? 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
[QUOTE=alex578344;45906740]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[/QUOTE] 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 [CODE]AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") include("shared.lua") function whenplayerspawn() print("Initial Spawn is working") end function Kill() Player:Kill() end[/CODE] Shared [CODE] function GM:Initialize() print("Hi!") hook.Add("PlayerInitialSpawn", "onPlayerSpawn", whenplayerspawn) hook.Add("InVehicle","inVehicle",Kill) end [/CODE]
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 [B]lua/shared.lua[/B] to [B]lua/autorun/myaddon_shared.lua[/B] [B]lua/init.lua[/B] to [B]lua/autorun/server/myaddon_init.lua[/B] [B]lua/cl_init.lua[/B] to [B]lua/autorun/client/myaddon_cl_init.lua[/B] 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 [code] 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 )[/code] cl_init.lua [code] 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 )[/code] shared.lua [code] 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. [/code] Your problem is that you do not know how to code.
Sorry, you need to Log In to post a reply to this thread.