Các đầu vào văn bản có giới hạn tần suất

*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.

Ví dụ này cho thấy cách thực hiện giới hạn tần suất 1 phút trên một trường nhập văn bản cho phép người chơi đăng ghi chú lên bảng thông báo cộng đồng.

Thêm đoạn mã sau vào một Script trong ServerScriptService:

Server

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Tạo RemoteEvent để giao tiếp
local postNoteEvent = Instance.new("RemoteEvent")
postNoteEvent.Name = "PostNoteEvent"
postNoteEvent.Parent = ReplicatedStorage
-- Giới hạn tần suất một ghi chú mỗi phút cho mỗi người chơi
local COOLDOWN_TIME = 60 -- Thời gian tính bằng giây
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
-- Xử lý yêu cầu đăng với giới hạn tần suất và lọc văn bản
postNoteEvent.OnServerEvent:Connect(function(player, noteData)
local canPost, cooldown = canPlayerPost(player)
if not canPost then
postNoteEvent:FireClient(player, "cooldown", math.ceil(cooldown))
return
end
-- Tất cả văn bản được chia sẻ công khai phải qua bộ lọc văn bản
local filterResult = getFilterResult(noteData.text, player.UserId)
if filterResult then
local success, filteredText = pcall(function()
return filterResult:GetNonChatStringForBroadcastAsync()
end)
if success then
-- Tạo ghi chú
local note = {
text = filteredText
}
-- Cập nhật thời gian đăng lần cuối
lastPostTime[player.UserId] = tick()
-- Gửi phản hồi thành công
postNoteEvent:FireClient(player, "success", note)
-- Phát sóng ghi chú mới đến tất cả người chơi
postNoteEvent:FireAllClients("newNote", note)
else
warn("Lỗi khi lọc văn bản!")
end
end)
-- Dọn dẹp khi người chơi rời khỏi
Players.PlayerRemoving:Connect(function(player)
lastPostTime[player.UserId] = nil
end)
©2026 Roblox Corporation. Roblox, logo Roblox và Powering Imagination là các nhãn hiệu đã đăng ký và chưa đăng ký của chúng tôi tại Hoa Kỳ và các quốc gia khác.