Hello,
I was looking for a way to allow players to only be allowed to talk in the chat every 5 seconds.
I don't know where to start with coding this, so any advice/help that you can give me would be appreciated.
Tweakified
To simply achieve this, I will be using one of Roblox's chat modules. To wait 5 seconds between sending a message, we will need to use a methodology named Debounce. A debounce will allow us to check if the player has chatted within 5 seconds or not. From this point on, I'm going to assume you know what it is. First of all, we will need to know what the function is that the client uses to send the servers its message(s) - Player > PlayerScripts > ChatScript > ChatMain > MessageSender. This is the module that carries the function that sends the server the message. It returns a metatable containing a function named SendMessage which is called when a player is trying to send a message to the server, so let's get that and assign it to our own function instead. Also, note that the metatable also contains the RemoteEvent that is used to send the message to the server. The following scripts needs to be local, I also want to make the scripts run as soon as the player joins, so these script will go in StarterPlayerScripts.
local player = game.Players.LocalPlayer local moduleChat = player.PlayerScripts:WaitForChild("ChatScript").ChatMain --the module containing all of the submodules local sender = require(moduleChat.MessageSender) --the submodule that handles server message sending requests (in a nutshell) --this function is called when the client tries to send a message to the server sender.SendMessage = function(self, message, toChannel) --self is our metatable containing everything initialized from when we required this module --message is the message the client is trying to send --toChannel is the channel the user is trying to send the message to self.SayMessageRequest:FireServer(message, toChannel) --tell the server to send this message to the server end
Okay, so now that we know how to set the function the client calls when it tries to make a message, we can add the functionality we need to to make it so that the player can only send a message every 5 seconds. This additional code is very identical to the article Roblox made about it.
local player = game.Players.LocalPlayer local moduleChat = player.PlayerScripts:WaitForChild("ChatScript").ChatMain local sender = require(moduleChat.MessageSender) local debounce = false sender.SendMessage = function(self, message, toChannel) if debounce then --if the debounce is true return --return so this scope can't continue any further end debounce = true --turn debounce on so this function can't be called and reach this point in the scope self.SayMessageRequest:FireServer(message, toChannel) --send this message to the server wait(5) --wait 5 seconds before turning debounce off again debounce = false --turn the debounce off so we can send a message again end
And that's how to allow players to talk every defined interval. I hope it helped!