Last Innocent

im trying to make a script that says [NAME] is the last innocent

here is my code



local function LastInno()
	inno = 0
	for k,v in pairs (player.GetAll() )
		if v:GetRole() == ROLE_INNOCENT then
			inno = inno + 1
		end
		if inno == 1 then
			name = v:Nick() -- just to get the last innos nick
		end
	end
	if inno == 1 then
		chat.AddText(Color(20,250,20,255)"[" .. name .. "]" .. Color(255,255,255,255) "Is the last innocent alive")
	end
end
hook.Add("MessageLastInno", "LastInnocent", LastInno)


any idea why its not working

[editline]25th July 2016[/editline]

there isn’t any errors

What is the event “MessageLastInno”

Change MessageLastInno to PlayerDeath and take a closer look at how hooks work.

Also, you should start using local variables instead of defining them globally.

Okay so here is my code now



function LastInno()
	local IsInnoCent = 0
	for k,v in pairs(player.GetAll() ) do
		if v:IsTerror() and v:GetRole() == ROLE_INNOCENT then
			-- print(v:GetRole()) -- wasn't working so added this to see what was going wronge
			IsInnoCent = IsInnoCent + 1
			-- print(IsInnoCent) -- wasn't working so added this to see what was going wronge
		end
		if IsInnoCent == 1 then
			local name = v:Nick() -- just to get the last innos nick
		end
	end
	if IsInnoCent == 1 then
		-- print("Chat Command is being called") -- wasn't working so added this to see what was going wronge
		chat.AddText(Color(20,250,20,255), "[", name, "]", Color(255,255,255,255), " Is the last innocent alive")
	end
end
hook.Add("PlayerDeath", "LastInnocent", LastInno)


and it gives me this error


 [ERROR] addons/myStiff/lua/autorun/lastinno.lua:15 attempt to index global 'chat' (a nil value)
	1.fn - addons/mystuff/lua/autorun/lastinno.lua:15

chat.AddText is Clientside, and the hook you’re using is Serverside. Use SendLua if you want to use chat.AddText

or put it under /client/… learn the basics of gLua before coding, please…