• Multi Gamemode Addon
    10 replies, posted
Alright, this might need some Context. Hi first of all. So, what im trying to create is a Addon that operates for several Gamemodes, while being one Addon. Like: On Sandbox it loads itself on the Sandbox Version i made, on DarkRP it loads the DarkRP version, on TTT it loads the TTT version, blah blah.. local GMName = gmod.GetGamemode() print("Addon has been loaded.") print("Searching for Gamemode Name...")         if GMName.Name == "Sandbox" then                  print("Gamemode Sandbox found")         chat.AddText(Color( 0, 159, 21),"Sandbox found!")         include ("gamemodes/gamemode_sandbox.lua")          elseif GMName.Name == "DarkRP" then                  print("Gamemode DarkRP found")         chat.AddText(Color( 0, 159, 21),"DarkRP found!")         include ("gamemodes/gamemode_darkrp.lua") end I guess you see what the Script is doing. Checking the Gamemode name, if the Gamemode name is DarkRP loads the darkrp version, if it sandbox its loading the sandbox version, and...you know what its doing. (the gamemode_sandbox.lua and gamemode_darkrp.lua are the files containing the whole addon in the different version) putting the files in the addons folder (in addons/addonfolder/lua/autorun/client/addon.lua and then starting a map i get a error, saying that GMName (gmod.GetGamemode()) is nil. However, if i edit something in the script and save, so the whole thing reloads, THEN it works, no script errors and its loading the addon for the right gamemode proberbly. Any idea why? My first guess was that the addon maybe loads too early so it cant get the gamemode name yet because it didnt finished loading proberbly yet? because it works if it rechecks when im in the map and can walk around. But if it loads automatrically like a lua in autorun is supposed to it doesnt work. Here is the whole script error for you: [ERROR] addons/addontest/lua/autorun/client/addon.lua:7: attempt to index local 'GMName' (a nil value)   1. unknown - addons/addontest/lua/autorun/client/addon.lua:7 In case anyone dont know what i mean or whats happening just ask. But i hope you got what i mean. Because to me it doesnt make much sense. It doesnt work at first load, but at reloading the addon or editing the script it works. Lel? Anyway, thanks for any help
try wrapping your code in hook.Add("PostGamemodeLoaded", "MultiGMAddon", function() --your code here end)
So you say the WHOLE thing local GMName = gmod.GetGamemode() print("Addon has been loaded.") print("Searching for Gamemode Name...") if GMName.Name == "Sandbox" then print("Gamemode Sandbox found") chat.AddText(Color( 0, 159, 21),"Sandbox found!") include ("gamemodes/gamemode_sandbox.lua") elseif GMName.Name == "DarkRP" then print("Gamemode DarkRP found") chat.AddText(Color( 0, 159, 21),"DarkRP found!") include ("gamemodes/gamemode_darkrp.lua") end should be in that function? or just a part of it?
hook.Add("PostGamemodeLoaded", "MultiGMAddon", function() local GMName = gmod.GetGamemode() print("Addon has been loaded.") print("Searching for Gamemode Name...") if GMName.Name == "Sandbox" then print("Gamemode Sandbox found") chat.AddText(Color( 0, 159, 21),"Sandbox found!") include ("gamemodes/gamemode_sandbox.lua") elseif GMName.Name == "DarkRP" then print("Gamemode DarkRP found") chat.AddText(Color( 0, 159, 21),"DarkRP found!") include ("gamemodes/gamemode_darkrp.lua") end end) Addons tend to load BEFORE the gamemode loads, which is why your gmod.GetGamemode() returns nil
alright so im theory that the addon loads too early was correct. lol XD BTW you have put the prints inside the hook. Which made them not print at all. I simply put them outside, before the hook. Now they print i nthe console like all the other addons. thx by the way. Now for some reason my name in chat is NULL..but thats not part of this thread. unless you can help me with that too
are you using any sort of chat addon?
nope. i develop all my addons in singleplayer (which is a server with 1 slot tho) and there i dont have any chat addons. By the way while i have you here, is there a way to make a chat.AddText only post a meesage i nthe chat ONCE AFTER you spawned? Not only is my message saying my name is NULL, it also apparently already prints in the chat in the middle of the loading screen. And then when i spawn its fading away already so yo ucant read the message...im assuming thats beause my first script (the searching for the right gamemode) is starting so early that it also starts the sandbox or darkrp version so early (and the main files contain thet chat.Addtext) and then they already prnt the message even tho i didnt even spawn yet
engine.ActiveGamemode Works at any stage. Returns the name of the folder of the gamemode. For instance TTT would be "terrortown".
??? local GMName = GAMEMODE_NAME print("Addon has been loaded.") print("Searching for Gamemode Name...") if GMName == "Sandbox" then print("Gamemode Sandbox found") chat.AddText(Color( 0, 159, 21),"Sandbox found!") include ("gamemodes/gamemode_sandbox.lua") elseif GMName == "DarkRP" then print("Gamemode DarkRP found") chat.AddText(Color( 0, 159, 21),"DarkRP found!") include ("gamemodes/gamemode_darkrp.lua") end
local GMName = engine.ActiveGamemode() print("Addon has been loaded.") print("Searching for Gamemode Name...") if GMName == "Sandbox" then print("Gamemode Sandbox found") chat.AddText(Color( 0, 159, 21),"Sandbox found!") include ("gamemodes/gamemode_sandbox.lua") elseif GMName == "DarkRP" then print("Gamemode DarkRP found") chat.AddText(Color( 0, 159, 21),"DarkRP found!") include ("gamemodes/gamemode_darkrp.lua") end As @Bobblehead said
alright. but i have hook.add now. Which works by simply typing if Gamemodename == "Trouble in Terrorist Town" i dont need the specific names for it. I just simply have the normal gamemodes name
Sorry, you need to Log In to post a reply to this thread.