Answered by
5 years ago Edited 5 years ago
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.
01 | local player = game.Players.LocalPlayer |
03 | local moduleChat = player.PlayerScripts:WaitForChild( "ChatScript" ).ChatMain |
04 | local sender = require(moduleChat.MessageSender) |
07 | sender.SendMessage = function (self, message, toChannel) |
11 | self.SayMessageRequest:FireServer(message, toChannel) |
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.
01 | local player = game.Players.LocalPlayer |
03 | local moduleChat = player.PlayerScripts:WaitForChild( "ChatScript" ).ChatMain |
04 | local sender = require(moduleChat.MessageSender) |
07 | sender.SendMessage = function (self, message, toChannel) |
14 | self.SayMessageRequest:FireServer(message, toChannel) |
And that's how to allow players to talk every defined interval. I hope it helped!