Help! Timer mess up.


if (SERVER) then
	util.AddNetworkString("SendHints");

	local hintDelay = 1200;

	local function Hints()
		net.Start("SendHints"); //send the message to display the hint.
		net.Broadcast();
	end


	hook.Add("Initialize", "SendHints", function() //hook into initialze to create a server timer that will be ran on all clients at the same time.
		Hints();

		timer.Create("SendHints", hintDelay, 1200, function()
			Hints(); //execute the function.
		end)
	end)

else

	//Definitions
	local hintsRussian = {

	}
	local hintsEnglish = {
		"Purge is un-active!";
		"Purge is active!";
	}



	net.Receive("SendHints", function()

		local isrus = GetConVarString("gmod_language") == "ru";
		local msg = "No hint!";
		if (isrus) then
			local ran = math.random(1, #hintsRussian);
			msg = hintsRussian[ran];
		else
			local num = math.random(1, #hintsEnglish);
			msg = hintsEnglish[num];
		end

		chat.AddText(Color( 50, 255, 50 ), "[Purge] ", Color( 255, 255, 255 ), msg ); //show the message;
	end)
	

end

It rapidly sends the hint instead of every 1200 seconds.

Because you call it in your hook function… Also, instead of setting it to repeat 1200 times, just make it 0 for infinite.

[editline]14th July 2014[/editline]

[lua]
hook.Add(“Initialize”, “SendHints”, function()
Hints(); // RIGHT HERE

	timer.Create("SendHints", hintDelay, 1200, function()
		Hints(); //execute the function.
	end)
end)

[/lua]

How do I fix this?

You remove the function call?

[lua]
hook.Add(“Initialize”, “SendHints”, function()
timer.Create(“SendHints”, hintDelay, 0, function()
Hints()
end)
end)
[/lua]


 if (SERVER) then
	util.AddNetworkString("SendHints");

	local hintDelay = 1200;

	local function Hints()
		net.Start("SendHints"); //send the message to display the hint.
		net.Broadcast();
	end




else

	//Definitions
	local hintsRussian = {

	}
	local hintsEnglish = {
		"Purge is un-active!";
		"Purge is active!";
	}



	net.Receive("SendHints", function()

		local isrus = GetConVarString("gmod_language") == "ru";
		local msg = "No hint!";
		if (isrus) then
			local ran = math.random(1, #hintsRussian);
			msg = hintsRussian[ran];
		else
			local num = math.random(1, #hintsEnglish);
			msg = hintsEnglish[num];
		end

		chat.AddText(Color( 50, 255, 50 ), "[Purge] ", Color( 255, 255, 255 ), msg ); //show the message;
	end)
	

end



Still not working…

You removed the hook now. All you had to do was remove Hint(); which you were calling before you made the timer, inside the hook.

Put the code I posted instead of the hook you had to start with.

Did that as well, post the full code? Something I’m probably messing up.

[lua]
if SERVER then
util.AddNetworkString(“SendHints”)

local hintDelay = 1200

hook.Add("Initialize", "SendHints", function()
	timer.Create("SendHints", hintDelay, 0, function()
		 //send the message to display the hint.
		net.Start("SendHints")
		net.Broadcast()
	end)
end)

else
//Definitions
local hintsRussian = {
“Something something”,
“Something somethingelse”
}

local hintsEnglish = {
	"Purge is un-active!",
	"Purge is active!"
}

net.Receive("SendHints", function()
	local isrus = GetConVarString("gmod_language") == "ru"
	local msg = "No hints!"
	
	if isrus then
		local ran = math.random(1, #hintsRussian)
		msg = hintsRussian[ran]
	else
		local num = math.random(1, #hintsEnglish)
		msg = hintsEnglish[num]
	end

	chat.AddText(Color( 50, 255, 50 ), "[Purge] ", Color( 255, 255, 255 ), msg ) //show the message;
end)

end
[/lua]

[editline]14th July 2014[/editline]

Also, how are you loading this?

[editline]14th July 2014[/editline]

How do you even know if it works? You need to wait 20 minutes for the hints to show up ?!

That didn’t work either, I’m loading it through lua/autorun file. Yes you need to wait 20 minutes!?!!?!

What isn’t working?

It’s still just repeating it in 1 second, instead of 20 minutes wait.

No it’s not.

[editline]14th July 2014[/editline]

You have restarted after you changed the code right?

Yes.

Well you’re doing something wrong… It’s not the code.
(I tested it)

I’m not doing anything wrong, sooooo.

Marking it dumb, makes you smart, great logic.

If you can’t take help don’t ask for it.

Here, I don’t know if this works, but it simplifies the code [kinda?]



local function sendhints()


	util.AddNetworkString("hints_send")


	local hintsRussian = {
		["nopurge"] = "Purge is un-active!",
		["yespurge"] = "Purge is active!"
	}


	local hintsEnglish = {
		["nopurge"] = "Purge is un-active!",
		["yespurge"] = "Purge is active!"
	}


	local hintdelay = 1200
	local ispurge = false
		
	timer.Create("hintsSendTimer", hintdelay, 0, function()
		if (ispurge) then
		net.Start("hints_send")
			net.WriteString(hintsEnglish["nopurge"])
			net.WriteString(hintsRussian["nopurge"])
		net.Broadcast()
		ispurge = !ispurge
		else
		net.Start("hints_send")
			net.WriteString(hintsEnglish["yespurge"])
			net.WriteString(hintsRussian["yespurge"])
		net.Broadcast()
		ispurge = !ispurge
		end
	end)
end
hook.Add("Initialize", "hintsthing", sendhints)


-- Somewhere clientside


net.Receive("hints_send", function()


	local purgeenglish = net.ReadString()
	local purgerussian = net.ReadString()
	local isrussian = GetConVarString("gmod_language") == "ru"
	
	if (isrussian) then
		chat.AddText(Color( 50, 255, 50 ), "[Purge] ", Color( 255, 255, 255 ), purgerussian )
	else
		chat.AddText(Color( 50, 255, 50 ), "[Purge] ", Color( 255, 255, 255 ), purgeenglish )
	end
end)


Also, I am the master of inefficient code.

Though it would be more effecient to have the strings clientside, since there’s no use for them on the serverside. Sending a bit telling which message to show would be a bit better.

[editline]14th July 2014[/editline]

I don’t really think it matters in this case… nvm.


	net.WriteString(hintsEnglish["nopurge"])
			net.WriteString(hintsRussian["nopurge"])

Wouldn’t this only display one of the messages, not the active message?

[editline]14th July 2014[/editline]

Shit, oops. Misread.



	local hintsRussian = {
		["nopurge"] = "Purge is un-active!",
		["yespurge"] = "Purge is active!"
	}


	local hintsEnglish = {
		["nopurge"] = "Purge is un-active!",
		["yespurge"] = "Purge is active!"
	}


You see how each variable in the table is defined with the key then the value, his code does a check to see if purge is on or off every x seconds, x being the delay, if it is on it sends the value at the key ‘nopurge’ which is “Purge is un-active” and then turns the purge off, and if it’s off, he sends the value of ‘yespurge’ which is “Purge is active” and turns the purge on.

So to answer your question, no, it sends the active message.