Hi, I’m working on a game that clones a map from game.ServerStorage and puts it into workspace. The problem is once I teleport the players onto it 2 seconds later sometimes the map hasn’t loaded in time and the players fall down… Is there a way to check when the map has loaded?
Side note: would putting the maps in game.ReplicatedStorage make a difference?
In my opinion, I’d leave maps in ServerStorage, advice would be let the map load in for a bit before teleporting them in. I usually move players into a map 5 second after loading the map up, I’ve never experienced a problem with that.
You could either,
- Listen to a descendant change in the workspace. If it is the map, teleport the players.
function CheckMapLoaded(Descendant)
if Descendant.Name == “NameOfMap” then
for i,v in pairs(game.Players:GetChildren()) do
v.Character:MoveTo(TeleportPosition)
end
end
end
game.Workspace.DescendantAdded:Connect(CheckMapLoaded)
*Use the WaitForChild in-built function to wait for it to load.
— part of the script where you teleport the player
local CurrentMap = workspace:WaitForChild(ClonedMap.Name)
—teleport the players
I haven’t tested it in this context but my Ready library may be able to help. Every time a descendant is added to the object it is being called on, the timeout is reset, and once the timeout is reached the object is considered ready. Not the greatest, but it’s the most I can do atm.
Haven’t tested this, but this should work in theory:
Load your maps in with a for loop. When you start loading the map in, tell the clients and have them set Workspace’s gravity to 0. Also tell the clients how many descendants the map has. Now, on each client, wait until the map in workspace has the same number of descendants. Then change gravity back to normal. Should prevent anyone from falling out of the map.
If this is the same thing that’s in RoStrap, I’ve had issues with this. It might be my fault, but I don’t think I was using it wrong. I actually built one of my projects around it to deal with loading of my interface and some other assets and noticed that it slowed down my game significantly compared to just using a lot of WaitForChild()
s
Though, maybe it’s not made for on-demand stuff
Yeah I intended it to be run at the start. I’ll look into optimising it more but I’m not sure how much I can improve it. It’s prime purpose was for parent objects, not specific things, also.
I used it in my moduled UI code to wait for my interface to be ready before doing stuff (the parent of it), so it was run at the start
Ah, okay, now that I think about it, it’s probably not a performance issue – it’s just how my library works. There’ll always be at least a 1 second delay for the object to be considered ready (if you have the default timeout value). There isn’t an actual way to guarantee an instance has loaded (and if there was, we probably wouldn’t need a user-made library to do something close to it lol). If you want something to be running instantly, you probably shouldn’t be using RbxReady.
Recently I’ve discovered something interesting – often WaitForChild (or RbxReady) isn’t needed anyway. In my latest project, I almost never use WaitForChild to get objects as by the time the script runs the objects already exist anyway. It’s weird and the behavior isn’t very well documented, I believe, but with some trial and error you’ll find a lot of the time you don’t actually need to care about things not existing. I don’t use WaitForChild in my UIs, for example. So the first-time setup situation no longer applies to RbxReady I guess.
I can provide some samples if you’re curious.
As for the usefulness of RbxReady in the OP’s scenario, though, I think it’ll be a good solution. The map loading is known to take longer than 1 second, which means the required 1 second default timeout will be unnoticeable.
Some others have already hinted at this, but honestly the best thing is having the client send an event to the server once the area has loaded in.
Theres a few ways to do this.
ondescendantadded is triggered as things are streamed to the client so you can reliably count parts and wait for things to finish loading. Its expensive though so you should deactivate it when you don’t need it.
Makes sense. Thanks for your explanation! I didn’t look into how the library actually works.
I also experience the same issue when players started to glitch and sit on seats, their avatars would get stuck on the spawning areas