Entradas de texto com limite de taxa

*Este conteúdo é traduzido por IA (Beta) e pode conter erros. Para ver a página em inglês, clique aqui.

Este exemplo mostra como implementar um limite de taxa de 1 minuto em um campo de entrada de texto que permite aos jogadores postar notas em um quadro de avisos da comunidade.

Adicione o seguinte a um Script em ServerScriptService:

Servidor

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Criar RemoteEvent para comunicação
local postNoteEvent = Instance.new("RemoteEvent")
postNoteEvent.Name = "PostNoteEvent"
postNoteEvent.Parent = ReplicatedStorage
-- Limite de taxa de uma nota por minuto por jogador
local COOLDOWN_TIME = 60 -- Tempo em 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
-- Manipular solicitações de postagem com limite de taxa e filtragem 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 texto compartilhado publicamente deve passar por um 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
-- Criar a nota
local note = {
text = filteredText
}
-- Atualizar o horário da última postagem
lastPostTime[player.UserId] = tick()
-- Enviar resposta de sucesso
postNoteEvent:FireClient(player, "success", note)
-- Transmitir nova nota para todos os jogadores
postNoteEvent:FireAllClients("newNote", note)
else
warn("Erro ao filtrar texto!")
end
end)
-- Limpar quando o jogador sair
Players.PlayerRemoving:Connect(function(player)
lastPostTime[player.UserId] = nil
end)
©2026 Roblox Corporation, Roblox, o logotipo Roblox e Powering Imagination estão entre nossas marcas registradas e não registradas nos EUA e em outros países.