この例では、プレイヤーがコミュニティ掲示板にノートを投稿できるテキスト入力フィールドに1分のレート制限を実装する方法を示します。
以下を Script に追加します ServerScriptService:
Server
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- 通信のためのRemoteEventを作成
local postNoteEvent = Instance.new("RemoteEvent")
postNoteEvent.Name = "PostNoteEvent"
postNoteEvent.Parent = ReplicatedStorage
-- プレイヤーごとに1分間に1つのノートのレート制限
local COOLDOWN_TIME = 60 -- 秒単位の時間
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
-- レート制限とテキストフィルタリングを伴う投稿リクエストを処理
postNoteEvent.OnServerEvent:Connect(function(player, noteData)
local canPost, cooldown = canPlayerPost(player)
if not canPost then
postNoteEvent:FireClient(player, "cooldown", math.ceil(cooldown))
return
end
-- 公開されるすべてのテキストはテキストフィルターを通過する必要があります
local filterResult = getFilterResult(noteData.text, player.UserId)
if filterResult then
local success, filteredText = pcall(function()
return filterResult:GetNonChatStringForBroadcastAsync()
end)
if success then
-- ノートを作成
local note = {
text = filteredText
}
-- 最後の投稿時間を更新
lastPostTime[player.UserId] = tick()
-- 成功レスポンスを送信
postNoteEvent:FireClient(player, "success", note)
-- 新しいノートをすべてのプレイヤーにブロードキャスト
postNoteEvent:FireAllClients("newNote", note)
else
warn("テキストフィルタリング中にエラーが発生しました!")
end
end)
-- プレイヤーが離れたときにクリーンアップ
Players.PlayerRemoving:Connect(function(player)
lastPostTime[player.UserId] = nil
end)