Hey, so i've been working on a gamemode and i've tried doing a cooldown for ooc messages for a while but i can't really figure out how to.
here's the ooc code, i would love it if someone could help me setup a cooldown between messages
function GM.Chat:ModifyForChatTypes( tblArgs, pPlayer, strText )
local find = string.find( strText, "//" )
if find == 1 then
_messageType = 1
strText = string.sub( strText, find +2 )
strText = string.Trim( strText )
table.insert( tblArgs, Color(143, 0, 255, 255) )
table.insert( tblArgs, "(OOC) " )
return strText, -1
end
why not just use a timer with a simple if statement?
i've tried but it didn't work, i'm new to lua and i thought it'd be great if someone could help me
well whats the code you used for the timer cooldown?
of course that doesnt work. You just create a timer, you never start it.
of all, use timer.Simple
your timer (if it would even run) executes a function that does absolutley nothing. you end it right after it began..
make a variable that is either true or false, and make the simple timer change its value after the 5 seconds. and then have the if statement check if the value is true (true for, should run)
let me rephrase that, i'm new to lua and i'm not sure what i'm doing here
could i have some noob friendly help please
im gonna give you pseudo code. Well im gonna write the code in here real quick, but it probably wont work. So you gotta rewrite it in actual lua syntax.
local check = true
if check then
check = false
-- do code stuff here
timer.Simple(5, function() check = true end)
i mean its really simple..you have a variable, and a if statement. thats a basic cooldown in coding gmod
Why are you using a timer? Just use CurTime/SysTime. Using a timer is pretty dumb for something like this.
Don't need to argue about method, more when people are new to lua. Its better to give them some easy-to-understand code, so they can learn something from it.
I tried this which in my eyes looks like it should work, still nothing, no errors, the timer just doesn't work
local check = true
if strText:sub( 1, 2 ) == "//" then
check = false
timer.Simple(30, function() check = true end)
if check then
return strText
else
print("cooldown")
end
end
that timer.simple needs to be INSIDE the if statement. not before it
i've tried all kinds of stuff, including that but still doesn't wanna work. the code below doesn't work
check = true
if strText:sub( 1, 2 ) == "//" then
check = false
if check then
check = false
timer.Simple(30, function() check = true end)
return strText
else
print("cooldown")
end
end
ok wait, this code seems fine to me. do you even run it in a chat hook?
yeah i've tried
GM.Chat:PlayerSay
and
GM.Chat:OnPlayerChat
PlayerSay is serverside
OnPlayerChat is clientside.
It depends what your addon is supposed to do. But since it is OOC chat, it should probably be serverside. make the first if statement true. the one that checks for "//". just write true so it definetly executes. see if it then works. then you know that something is wrong with that condition
The CurTime method is simpler though? It's just setting a variable to CurTime() + cooldown_time and checking if the variable's smaller than CurTime() in an if statement.
local cooldown = 10
hook.Add("PlayerSay", "psay_ooc", function(ply, text)
if not text:StartWith("//") or ply.oocCooldown then return end
local msg = text:TrimLeft("//")
broadcastOOC(ply, msg)
ply.oocCooldown = true
timer.Simple(cooldown, function()
ply.oocCooldown = false
end)
end)
local cooldown = 10
hook.Add("PlayerSay", "psay_ooc", function(ply, text)
if not text:StartWith("//") or (ply.cooldownTime and ply.cooldownTime > CurTime()) then return end
local msg = text:TrimLeft("//")
broadcastOOC(ply, msg)
ply.cooldownTime = CurTime() + cooldown
end)
Here’s some examples using both of the said methods. Which you choose is up to you as there are no real differences. I also typed this on my phone and couldn’t actually run the code so there may be some syntax errors or something, but the concepts remain the same.
(broadcastOOC is obviously just a net message broadcasting the msg to all clients)
Sorry, you need to Log In to post a reply to this thread.