Help! How do I create a toggle feature?

I am trying to create a toggle feature to stop the spam in this script:


timer.Create( "my_timer", 0.5, 0, function()
	
	RunConsoleCommand("say", "Jimmy the Skid")

	end

end )

It spams a piece of text into chat and I want a toggle feature.

Thanks!

if i understand your question right



local enabled = true

concommand.Add("spamchat",function() enabled = not enabled print("Chatspam set to "..tostring(enabled)) end)

timer.Create( "my_timer", 0.5, 0, function()
	if not enabled then return end
	RunConsoleCommand("say", "Jimmy the Skid")

	end

end )

if u find any errors you may keep them

Controlled toggles come in many different forms… https://dl.dropboxusercontent.com/u/26074909/tutoring/logic/controlled_toggles.lua.html

It just depends how you want the action to happen and how you want to reset it. The second example shows a toggle bound by a key and the only way to reset it is if it was triggered and the key is released, likewise the only way to trigger is if it isn’t triggered and the key is pressed. Both scenarios ensure it is only triggered once.

Ok thanks guys!

–EDIT–


[ERROR] lua/idk.lua:21: ')' expected (to close '(' at line 15) near 'end'
  1. unknown - lua/idk.lua:0

Argh :confused:

2ND EDIT

Nvm, fixed it.

[lua]
function NoPlayersMessage()
if timer.Exists(“NoPlayersTimer”) then return end
timer.Create( “NoPlayersTimer”, 180 , 0, function()
if #player.GetAll() > 1 then return end
PrintMessage( HUD_PRINTTALK, “Not enough players to start the game.” )
end )
end
[/lua]

That is a snippet from my murder gamemode, modify it to your needs.