Loading objects in and out of the workspace based on level?

I’m creating a platformer and I have a folder with all of the hazards in it. Currently I just get all of those and damage the player if they touch anything within it, but I want to load sections of these stages in and out when a player transitions to a new one and at the moment I cannot remove the hazzard objects in any given part because they are all in one folder. Could I still just get a return of all of those if I did something like “Stages, 1-1, hazards”?

Why don’t you try to use streaming enabled? You can make it, so it only loads what is near the player. And if that isn’t what you want you can always divide a level in chunks/load all chunks at once. All the parts that you don’t want to load you could parent them to PlayerGui or so.

If your concern is that you won’t be able to load it from the server then you can do what you suggest. If you wish to get the objects (hazards) and have them separated you can always use a localization script.

I commonly add an attribute to a group of Instances I want to control, for example I can add an attribute called “ID” with a value of “Hazards” and in the script you will be able to get them by doing this:

local getInstancesWithID = function(ID)
    local returnValue = {};

    for _, i in pairs(game:GetDescendants()) do
        pcall(function()
            if(i:GetAttribute("ID") == ID) then table.insert(returnValue, i) end; 
        end);
    end;

    return returnValue;
end;

getInstancesWithID("Hazzards"); --Gets all instances in the game with that ID
1 Like

Would I need to add that attribute to every hazard object? I’m pretty new to the whole attributes thing I don’t think I’ve used it before

Yes, the good thing is that you can just bulk select the entire folder (Open it then click the first one and shift click the last one) and then add that attribute.

And if you are wondering how it would go in relation to coding I am pretty sure you can just do the same loop:

local getInstancesWithID = function(ID)
    local returnValue = {};

    for _, i in pairs(game:GetDescendants()) do
        pcall(function()
            if(i:GetAttribute("ID") == ID) then table.insert(returnValue, i) end; 
        end);
    end;

    return returnValue;
end;

for _, h in pairs(getInstancesWithID("Hazzards")) do
    if(h:IsA("BasePart")) then --We ensure it is a base part.
        --Code for hazzard here
    end;
end;
1 Like

Alright thank you so much! I’ll try this out.

1 Like

Nothing seems to be happening when I put this anywhere, no errors or anything it just doesn’t do what I want it to do

Does it not load the hazzards?

no nothing at all happens , anything I put in the “code for hazards here” section doesn’t run

I’m not exactly sure what you mean but if you wanted a system for removing obstacles that aren’t showing you could check if the hazard is in the camera view and not hidden behind anything. If this is what you’re looking for then I can link another post showing how to accomplish it.

Can you show me the attributes in the hazard block?

That would work too. I just want to not have everything always in the world at once

Then why don’t you use streaming enabled?

In workspace go to the properties and enable this:
image

Then you can change the settings a bit.

I do not know what that is I have rather limited scripting knowledge

image
this is the only one I have

There should be an attribute with a name called “ID” the value of “ID” is Hazzards

Start:
image

2nd:
image

3rd:
image

4th:
image

this seems to break several features that require a humanoidrootpart for some reason when I test thegame

Hmm, I understand why. I believe these animations don’t load since they are very far away.

Basically what StreamingEnabled does is make it so parts that are far away don’t load until the player gets near them. And you can modify them so it also unloads the parts if they are far.

Did the ID attribute work though?

Yeah I have some power ups and a trail that activates when the player jumps and none of these things work when I have it enabled. And yeah the attributes work

1 Like