y453bh Posted April 7, 2020 Share Posted April 7, 2020 (edited) hello! I don't want it to kick people, just that it doesn't allow you to do a command more than 1 time in a certain time, thanks! Edited April 7, 2020 by y453bh Link to comment
Moderators Patrick Posted April 7, 2020 Moderators Share Posted April 7, 2020 Check example on onPlayerCommand Link to comment
y453bh Posted April 7, 2020 Author Share Posted April 7, 2020 2 minutes ago, Patrick said: Check example on onPlayerCommand I dont want it to kick you, just that appear a message saying "Wait 10 seconds", but not for every command, just some Link to comment
Moderators Patrick Posted April 7, 2020 Moderators Share Posted April 7, 2020 Just replace kickPlayer function to your outputChatBox. Link to comment
y453bh Posted April 7, 2020 Author Share Posted April 7, 2020 (edited) Just now, Patrick said: Just replace kickPlayer function to your outputChatBox. It doesnt work already tried it, also I told you that i want some commands, not all, i dont know how to do it Edited April 7, 2020 by y453bh Link to comment
Moderators Patrick Posted April 7, 2020 Moderators Share Posted April 7, 2020 1 minute ago, y453bh said: It doesnt work already tried it, also I told you that i want some commands, not all, i dont know how to do it local CMD_INTERVAL = 200 --// The interval that needs to pass before the player can execute another cmd local KICK_AFTER_INTERVAL = 50 --// Kick the player if they execute more than 20 cmds/sec local limitedCommands = { ["something"] = true, ["othercommand"] = true, -- list of limited commands } local executions = setmetatable({}, { --// Metatable for non-existing indexes __index = function(t, player) return getTickCount()-CMD_INTERVAL end }) addEventHandler("onPlayerCommand", root, function(command) if limitedCommands[command] then if (executions[source]-getTickCount()<=CMD_INTERVAL) then if (executions[source]-getTickCount()<=KICK_AFTER_INTERVAL) then outputChatBox("Don't flood", source) end cancelEvent() end executions[source] = getTickCount() end end ) Link to comment
y453bh Posted April 7, 2020 Author Share Posted April 7, 2020 Just now, Patrick said: local CMD_INTERVAL = 200 --// The interval that needs to pass before the player can execute another cmd local KICK_AFTER_INTERVAL = 50 --// Kick the player if they execute more than 20 cmds/sec local limitedCommands = { ["something"] = true, ["othercommand"] = true, -- list of limited commands } local executions = setmetatable({}, { --// Metatable for non-existing indexes __index = function(t, player) return getTickCount()-CMD_INTERVAL end }) addEventHandler("onPlayerCommand", root, function(command) if limitedCommands[command] then if (executions[source]-getTickCount()<=CMD_INTERVAL) then if (executions[source]-getTickCount()<=KICK_AFTER_INTERVAL) then outputChatBox("Don't flood", source) end cancelEvent() end executions[source] = getTickCount() end end ) The message appear but I still can make the command Link to comment
Moderators IIYAMA Posted April 7, 2020 Moderators Share Posted April 7, 2020 Interesting solution @Patrick , I am hardly using metatables so I have always trouble to get around the default table logic. btw I am wondering is the rawset function not required to set the value on line 26? Link to comment
Moderators Patrick Posted April 7, 2020 Moderators Share Posted April 7, 2020 4 minutes ago, IIYAMA said: Interesting solution @Patrick , I am hardly using metatables so I have always trouble to get around the default table logic. This code from onPlayerCommand's wiki page, I just edited and put the command name check inside it. 6 minutes ago, IIYAMA said: btw I am wondering is the rawset function not required to set the value on line 26? Hmm, looks like unnecessary. 1 Link to comment
y453bh Posted April 7, 2020 Author Share Posted April 7, 2020 Help @Patrick it still let me do the command Link to comment
Moderators Patrick Posted April 7, 2020 Moderators Share Posted April 7, 2020 (edited) 13 minutes ago, y453bh said: Help @Patrick it still let me do the command Here is my solution, I hope its good for you. local LIMITED_COMMANDS = { -- ["COMMAND_NAME"] = TIME_LIMIT_IN_SEC ["something"] = 5, -- Can use this command only once in every 5 sec ["spam"] = 10, -- Can use this command only once in every 10 sec } local EXEC = {} addEventHandler("onPlayerCommand", root, function(cmd) local limit_sec = LIMITED_COMMANDS[cmd] if not limit_sec then return end local tick = getTickCount() if not EXEC[source] then EXEC[source] = {} end if not EXEC[source][cmd] then EXEC[source][cmd] = 0 end if EXEC[source][cmd] + (limit_sec * 1000) > tick then cancelEvent() return outputChatBox("#FF0000[ANTISPAM]#FFFFFF You can use this command once, in every "..limit_sec.." sec.", source, 255, 255, 255, true) end EXEC[source][cmd] = tick end) addEventHandler("onPlayerQuit", root, function() EXEC[source] = nil end) Edited April 7, 2020 by Patrick Link to comment
y453bh Posted April 8, 2020 Author Share Posted April 8, 2020 7 hours ago, Patrick said: Here is my solution, I hope its good for you. local LIMITED_COMMANDS = { -- ["COMMAND_NAME"] = TIME_LIMIT_IN_SEC ["something"] = 5, -- Can use this command only once in every 5 sec ["spam"] = 10, -- Can use this command only once in every 10 sec } local EXEC = {} addEventHandler("onPlayerCommand", root, function(cmd) local limit_sec = LIMITED_COMMANDS[cmd] if not limit_sec then return end local tick = getTickCount() if not EXEC[source] then EXEC[source] = {} end if not EXEC[source][cmd] then EXEC[source][cmd] = 0 end if EXEC[source][cmd] + (limit_sec * 1000) > tick then cancelEvent() return outputChatBox("#FF0000[ANTISPAM]#FFFFFF You can use this command once, in every "..limit_sec.." sec.", source, 255, 255, 255, true) end EXEC[source][cmd] = tick end) addEventHandler("onPlayerQuit", root, function() EXEC[source] = nil end) Thanks for your help, but I added my script command and still can use it, also added the "me" command and I can't use it, the same message everytime ... Link to comment
Moderators Patrick Posted April 8, 2020 Moderators Share Posted April 8, 2020 12 hours ago, y453bh said: Thanks for your help, but I added my script command and still can use it, also added the "me" command and I can't use it, the same message everytime ... First of all... stop spamming... And provide more informations about what are you doing, because it has to work. Link to comment
y453bh Posted April 8, 2020 Author Share Posted April 8, 2020 (edited) Can you give me the lines for antispam only?, so I can add them to all my scripts? Because this way isnt't working, please @Patrick Edited April 8, 2020 by y453bh Link to comment
Moderators Patrick Posted April 8, 2020 Moderators Share Posted April 8, 2020 (edited) 38 minutes ago, y453bh said: Can you give me the lines for antispam only?, so I can add them to all my scripts? Because this way isnt't working, please @Patrick Oh.. you can't cancel client sided commands. Then, here is one way, how to limit commands on client side: local EXEC = {} function canUse(cmd, limit_sec) if not EXEC[cmd] then EXEC[cmd] = 0 end local lastuse = EXEC[cmd] local tick = getTickCount() if lastuse + ((limit_sec or 2) * 1000) > tick then return false end EXEC[cmd] = tick return true end -- EXAMPLE function yourFunction(cmd) -- Need to add this part to your functions if not canUse(cmd, 2) then return outputChatBox("#FF0000[ANTISPAM]#FFFFFF You can use this command once, in every 2 sec!") end -- your code ... end addCommandHandler("asd", yourFunction) Edited April 8, 2020 by Patrick Link to comment
y453bh Posted April 8, 2020 Author Share Posted April 8, 2020 37 minutes ago, Patrick said: Oh.. you can't cancel client sided commands. Then, here is one way, how to limit commands on client side: local EXEC = {} function canUse(cmd, limit_sec) if not EXEC[cmd] then EXEC[cmd] = 0 end local lastuse = EXEC[cmd] local tick = getTickCount() if lastuse + ((limit_sec or 2) * 1000) > tick then return false end EXEC[cmd] = tick return true end -- EXAMPLE function yourFunction(cmd) -- Need to add this part to your functions if not canUse(cmd, 2) then return outputChatBox("#FF0000[ANTISPAM]#FFFFFF You can use this command once, in every 2 sec!") end -- your code ... end addCommandHandler("asd", yourFunction) That's what I needed, thanks a lot @Patrick Link to comment
Alfredo Posted April 9, 2020 Share Posted April 9, 2020 Here is another one... you need just add the cmd to "cmdAntiSPAM" table like the example: local cmdAntiSPAM = { -- {cmd,usageLimit,time,dataName}, {"say",5,3000,"CMD_SAY"}, {"help",1,3000,"CMD_HELP"}, } function preventCommandSpam(command) for i,v in ipairs(cmdAntiSPAM) do if v[1] == command then local data = getElementData(source,v[4]) if data then local spam = data+1 if spam >= v[2] then cancelEvent() outputChatBox ("You need to wait before using this command again!", source, 255, 0, 0, false) else setElementData(source,v[4],spam) end else setElementData(source,v[4],0) setTimer(removeSPAMData,v[3],1,source,v[4]) end end end end addEventHandler("onPlayerCommand", getRootElement(), preventCommandSpam) function removeSPAMData(player,data) if isElement(player) then removeElementData(player,data) end end I hope this can help you. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now