Files
block-game/ServerScriptService/CmdrCommands/ChunkCullWorld.lua
2026-01-08 22:58:58 +02:00

80 lines
2.2 KiB
Lua

return {
Name = "chunkcull",
Aliases = {"cullchunks", "resetchunks"},
Description = "Unload all server chunk cache instantly, then preload only chunks near players (and force clients to unload/resync).",
Group = "Admin",
Args = {
{
Type = "integer",
Name = "radius",
Description = "Horizontal chunk radius around each player to preload",
Optional = true,
Default = 5,
},
{
Type = "integer",
Name = "yRadius",
Description = "Vertical chunk radius around each player to preload",
Optional = true,
Default = 1,
},
},
Run = function(context, radius, yRadius)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local terrainGen = require(
game:GetService("ServerScriptService")
:WaitForChild("Actor")
:WaitForChild("ServerChunkManager")
:WaitForChild("TerrainGen")
)
local tickRemote = ReplicatedStorage:WaitForChild("Tick")
local r = radius or 5
local ry = yRadius or 1
local unloaded = 0
pcall(function()
unloaded = terrainGen:UnloadAllChunks()
end)
-- Tell all clients to immediately drop their local chunk instances
pcall(function()
tickRemote:FireAllClients("U_ALL", 0, 0, 0, 0, 0, 0, 0)
end)
-- Preload server chunks around players (reduces initial lag spikes after cull)
local preloaded = 0
pcall(function()
preloaded = terrainGen:PreloadNearPlayers(r, ry)
end)
-- Force clients to resync around themselves
local resyncCount = 0
for _, player in ipairs(Players:GetPlayers()) do
local character = player.Character
local root = character and character:FindFirstChild("HumanoidRootPart")
if root then
local pos = root.Position
local cx = math.floor((pos.X + 16) / 32)
local cy = math.floor((pos.Y + 16) / 32)
local cz = math.floor((pos.Z + 16) / 32)
for y = -ry, ry do
for x = -r, r do
for z = -r, r do
tickRemote:FireClient(player, "C_R", cx + x, cy + y, cz + z, 0, 0, 0, 0)
resyncCount += 1
end
end
end
end
end
return (
"chunkcull done | unloaded=%d | preloaded=%d | resyncPackets=%d | radius=%d yRadius=%d"
):format(unloaded, preloaded, resyncCount, r, ry)
end,
}