هذا المثال يوضح كيفية تنفيذ حد معدل قدره دقيقة واحدة على حقل إدخال نصي يسمح للاعبين بنشر ملاحظات على لوحة إعلانات المجتمع.
أضف ما يلي إلى 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
-- حد معدل لملاحظات واحدة في الدقيقة لكل لاعب
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)