local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local KnightRemotes = require(ReplicatedStorage.Packages.knightremotes)
KnightRemotes:init()
-- Setup remotes
KnightRemotes:new("PlayerJoined", true, false)
KnightRemotes:new("GetInventory", true, true)
KnightRemotes:new("EquipItem", true, true)
KnightRemotes:new("UpdatePosition", false, false) -- Unreliable for performance
-- Rate limiting middleware
local requestCounts = {}
KnightRemotes:UseMiddleware(function(player, eventName, args)
if not player then return true end
local key = player.UserId
requestCounts[key] = (requestCounts[key] or 0) + 1
if requestCounts[key] > 100 then
player:Kick("Too many requests")
return false
end
return true
end)
-- Reset rate limits every second
task.spawn(function()
while true do
task.wait(1)
requestCounts = {}
end
end)
-- Handle inventory requests
KnightRemotes:Connect("GetInventory", function(player)
local inventory = player:FindFirstChild("Inventory")
if inventory then
return true, inventory:GetChildren()
else
return false, "Inventory not found"
end
end)
-- Handle item equipping
KnightRemotes:Connect("EquipItem", function(player, itemId)
local inventory = player:FindFirstChild("Inventory")
local item = inventory and inventory:FindFirstChild(itemId)
if item then
-- Equip logic here
return true, "Item equipped"
else
return false, "Item not in inventory"
end
end)
-- Broadcast player positions to nearby players
KnightRemotes:Connect("UpdatePosition", function(player, position)
-- Get nearby players
local nearbyPlayers = {}
for _, otherPlayer in pairs(game.Players:GetPlayers()) do
if otherPlayer ~= player then
local char = otherPlayer.Character
local otherPos = char and char:GetPrimaryPartCFrame().Position
if otherPos and (otherPos - position).Magnitude < 100 then
table.insert(nearbyPlayers, otherPlayer)
end
end
end
-- Send position update to nearby players only
for _, nearbyPlayer in pairs(nearbyPlayers) do
KnightRemotes:Fire("PlayerMoved", nearbyPlayer, player.Name, position)
end
end)