Coding for Streaming Enabled

I have parts that are not loaded on my client-side because of streaming enabled. However, those parts have code that’s loaded during runtime. Is there a way to have these items scripted while running streaming enabled?

From the streaming enabled topic, coefficient stated:

Clients usually will not have the entire Workspace available locally. Developers must ensure that game functionality works when clients are missing parts of the world, such as by using Instance:WaitForChild (Please be aware that there is a chance WaitForChild will hang forever if the instance being waited for is never streamed in and that a timeout in WaitForChild is not specified).

In this case, your parts need to be yielded, e.g through WaitForChild with a timeout before being streamed in. If some of your parts are never streamed in, then you code will not be able to run during runtime.

A good way of handling objects that are parented or removed due to StreamingEnabled is by keeping track of:

  • ChildAdded
  • Parent

Example:

local Wall = workspace:WaitForChild("Wall") -- maybe you should add a timeout to avoid infinite yielding

-- when the object is re-parented to workspace and you can see it
workspace.ChildAdded:Connect(function(Child)
   if Child.Name == "Wall" then
     Wall = Child
   end
end)

-- when the object is removed from the workspace
Wall:GetPropertyChangedSignal("Parent"):Connect(function()
  -- DoSomething()
end)

-- you should disconnect the events when no longer needed though.

This module created by Sleitnick already handles it.
Knit/Streamable.lua at main · Sleitnick/Knit · GitHub

7 Likes

this is great! thanks for sharing!