Este ejemplo muestra cómo implementar un límite de tasa de 1 minuto en un campo de entrada de texto que permite a los jugadores publicar notas en un tablón de anuncios comunitario.
Agrega lo siguiente a un Script en ServerScriptService:
Servidor
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Crear RemoteEvent para comunicación
local postNoteEvent = Instance.new("RemoteEvent")
postNoteEvent.Name = "PostNoteEvent"
postNoteEvent.Parent = ReplicatedStorage
-- Límite de tasa de una nota por minuto por jugador
local COOLDOWN_TIME = 60 -- Tiempo en segundos
local lastPostTime = {}
local function canPlayerPost(player)
local userId = player.UserId
local currentTime = tick()
if lastPostTime[userId] then
local timeSinceLastPost = currentTime - lastPostTime[userId]
if timeSinceLastPost < COOLDOWN_TIME then
return false, COOLDOWN_TIME - timeSinceLastPost
end
end
return true, 0
end
-- Manejar solicitudes de publicación con límite de tasa y filtrado de texto
postNoteEvent.OnServerEvent:Connect(function(player, noteData)
local canPost, cooldown = canPlayerPost(player)
if not canPost then
postNoteEvent:FireClient(player, "cooldown", math.ceil(cooldown))
return
end
-- Todo el texto compartido públicamente debe pasar por un filtro de texto
local filterResult = getFilterResult(noteData.text, player.UserId)
if filterResult then
local success, filteredText = pcall(function()
return filterResult:GetNonChatStringForBroadcastAsync()
end)
if success then
-- Crear la nota
local note = {
text = filteredText
}
-- Actualizar el tiempo de la última publicación
lastPostTime[player.UserId] = tick()
-- Enviar respuesta de éxito
postNoteEvent:FireClient(player, "success", note)
-- Difundir la nueva nota a todos los jugadores
postNoteEvent:FireAllClients("newNote", note)
else
warn("¡Error al filtrar el texto!")
end
end)
-- Limpiar cuando el jugador se va
Players.PlayerRemoving:Connect(function(player)
lastPostTime[player.UserId] = nil
end)