TTT Override radio commands

I wrote a small addon to overwrite the radio function. I did this so I can prevent people saying that they’re with someone in spectator deathmatch or someone who is cloaked. It works perfectly when I overwrite the gamemode files, but I can’t when I run it as an addon it doesn’t work. I get this error when it runs

[ERROR] addons/radiofix/lua/autorun/radiofix.lua:3: attempt to index global 'RADIO' (a nil value)
  1. fn - addons/radiofix/lua/autorun/radiofix.lua:3
   2. unknown - addons/ulib/lua/ulib/shared/hook.lua:183

I completely understand that RADIO is undefined in this file, but shouldn’t I be able to use RADIO because its declared globally in cl_voice.lua?

RADIO = {}

Heres the code for my addon. I modify the filter to skip the ghosts in deathmatch (another addon) and then I replace someones name with “someone who is cloaked” for someone using a cloaking device.

hook.Add( "Initialize", "SpecCalloutOverride", function()

	function RADIO:GetTargetType()
		if not IsValid(LocalPlayer()) then return end
	 --local trace = LocalPlayer():GetEyeTrace(MASK_SHOT)

		local tab = util.GetPlayerTrace( LocalPlayer() )

		 tab.filter = function(ent)
										if ent:IsPlayer() and ent:IsSpec() then
												return false
										elseif ent == LocalPlayer() then
												return false
											return true

		 local trace = util.TraceLine(tab)


		 if not trace or (not trace.Hit) or (not IsValid(trace.Entity)) then return end

		 local ent = trace.Entity

		 if ent:IsPlayer() then
				if ent:GetNWBool("disguised", false) then
					 return "quick_disg", true
				elseif ent:GetNWBool("cloaked", false) then
					return "someone who is cloaked.", true
					 return ent, false

		 elseif ent:GetClass() == "prop_ragdoll" and CORPSE.GetPlayerNick(ent, "") != "" then

				if DetectiveMode() and not CORPSE.GetFound(ent, false) then
					 return "quick_corpse", true
					 return ent, false

You’re probably calling RADIO before it’s defined (the hook is running to early). You could just define it in the Init hook before the function uses it, then comment out the other definition. That, or load it after RADIO is defined.

Thanks, do you know a way that I can insure that RADIO isn’t called before its defined? I am trying to avoid editing the gamemode since I don’t want an update causing potential errors.

Use a different hook or a timer to make sure that RADIO is defined first.

Thanks a lot, I used a different hook and it works perfectly now. Heres the completed addon if you’re interested