Streaming enabled event?

Is there an event or some kind that will fire when a new chunk is loaded or unloaded in a large game using streaming enabled? I ask because i am building a system of npc’s and, whenever the character resets or the character loads into the game, it fires the local functions for the npc’s. It does this by going through a loop and firing the function several times. But, if using streaming enabled, all the recources will not be available, so not all the npcs will be indexed. If theres some kind of event that will detect if a new chunk is loaded and to refire the function when that heppens, that would most definitely solve my problem. Or if theres another way anyone can think of doing it. I use heartbeat a lot, in a bunch of different contexts, such as Detecting if a player is in a region. I wana avoid using too many heartbeat functions for lag reasons. Unless i’m being dumb, and it will only create lag if in the same script… this is all local.

1 Like

I don’t believe there is such an event. If WaitForChild won’t do the job for you then how about the DescendantAdded event? although I’m not sure if that fires when objects are streamed in.

The only problem is WaitForChild may timeout if the model isnt found. That and when the game starts it loops through and NPC Folder, and then runs the same core function for each… so nomatter what npc you approach, it will run… but… for npcs that arent loaded yet it wont run within the loop.

Correct me if I’m wrong, but I am very sure that WaitForChild will not time out unless you give the second timeout argument, it will just show a warning that it may yield indefinitely, which will not be the case if WaitForChild does find the part.

You could probably test this with some small code:

-- spawn a thread 7 seconds in advance, after 5.
delay(7, function()
    local p = Instance.new("Part")
    p.Name = "RandomName"
    p.Parent = workspace
end)

-- print the part name when it exists.
print(workspace:WaitForChild("RandomName"))

-- warning first,
--> Infinite yield possible on 'Workspace:WaitForChild("RandomName")'

-- prints the part next,
--> RandomName

If you want to remove the warnings, you could just make the timeout argument very large, like math.huge.

If you want to run a client-sided loop that needs to be run before the NPCs are loaded in, you can either make that code server-sided, or you could keep track of the logic without the instance, or even instantiate a fake instance that gets replaced when the server’s is loaded in.

If you want to keep track of all NPCs in an NPC folder after calling :GetChildren, you could connect a .ChildAdded event that runs the core function:

for _, child in ipairs(NPCFolder:GetChildren()) do
    RunCoreFunction(child)
end

NPCFolder.ChildAdded:Connect(function(child)
    RunCoreFunction(child)
end)

-- OR

-- NPCFolder.ChildAdded:Connect(RunCoreFunction)

You can also do this with CollectionService in a similar way, and as far as I know, it will still account for instances being streamed in:

local CollectionService = game:GetService("CollectionService")

for _, npc in ipairs(CollectionService:GetTagged("NPC")) do
    RunCoreFunction(npc)
end
CollectionService:GetInstanceAddedSignal("NPC"):Connect(function(npc)
    RunCoreFunction(npc)
end)
1 Like

You can keep your NPCs in a Folder titled “NPCs” or something and use Instance.ChildAdded to detect when an NPC is loaded. You can also use this and the fact that the streaming grid uses 16x16 chunks to detect when a chunk is loaded using parts.

1 Like