Far Behind Chunk Loading

Is there anyway to make a script that unloads chunks that are far behind the player? I know streaming enabled exists but I’m not sure it does this. Any ideas?

Your can use the DistanceFromCharacter() function.

Example:

LocalScript:

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local UnloadPart = workspace:WaitForChild("Part") -- The part that you want to unload.

local Player = Players.LocalPlayer

local MaxDistance = 100 -- How many studs do you want to unload it from the character.

--// Functions:

function RemoveOnDistance() -- The main function which will unload the chunk
    if Player:DistanceFromCharacter(UnloadPart.Position) >= MaxDistance then -- Player:DistanceFromCharacter(UnloadPart.Position) returns the studs from your character to the part.
        UnloadPart.Transparency = 1 -- Makes it less physically rendered.
        UnloadPart.CanCollide = false -- Making it noncollision to make it feel less and also fewer physics calculations required
    end
end

RunService.Stepped:Connect(RemoveOnDistance) -- Activating our main function.

If you wanna make it for multiple parts then you would do it like this:

LocalScript:

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local UnloadParts = workspace:WaitForChild("FolderWithParts") -- The part that you want to unload.

local Player = Players.LocalPlayer

local MaxDistance = 100 -- How many studs do you want to unload it from the character.

--// Functions:

function RemoveOnDistance() -- The main function which will unload the chunk
    for _, Part in pairs(UnloadParts:GetChildren()) do -- Getting all the childrens from the folder.
        if Player:DistanceFromCharacter(UnloadPart.Position) >= MaxDistance then -- Player:DistanceFromCharacter(UnloadPart.Position) returns the studs from your character to the part.
            Part.Transparency = 1 -- Makes it less physically rendered.
            Part.CanCollide = false -- Making it noncollision to make it feel less and also fewer physics calculations required
        end
    end
end

RunService.Stepped:Connect(RemoveOnDistance) -- Activating our main function.

Hope it helps you understand it :slight_smile:

If you wanna know more about what I explained here, go to:

DistanceFromCharacter: Player | Roblox Creator Documentation

1 Like

Alright, I’ll test this out. Thanks!

Sorry for the bump, but wouldn’t changing the transparency be more performance expensive than deleting the instance?

He want’s to unload, Which means that when he comes near again it should, load it back.

Yeah, but I mean performance wise be ais3 surely changing the Transparency to 1 would be less efficient than removing the instance and reloading it when needed?

I’m pretty sure it’s more performant to change the Transparency to 1 instead of caching an object somewhere and cloning them back to Workspace.

But if I’m wrong, feel free to correct me.

2 Likes

I mean if you’re right then it helps my chunk loading system so am not complaining, am querying :thinking: