Server Scripts cant read children of a model imported from InsertService

  1. What do I want to achieve? Fix my Map loading system

  2. What is the issue? Recently, my map loader has stopped functioning in the way it’s intended to. Everything about it works except for the model being cloned from the folder in Replicated storage, into the Folder on the workspace when the round system starts up (Code example below this description).

When the model is sent from the folder in Replicated storage into the folder on the workspace, none of its children are sent along with it, and it arrives as an empty model. In fact, the empty model was not even visible to me in the workspace until I scripted it to be parented into the workspace specifically.

  1. What solutions have I tried so far? Ive tried adjusting my game’s ModelStreaming settings because I found out that certain settings could prevent the model from being visible, but none of those worked.

Ui botton that triggers the Insertion

local button = script.Parent
local textLabel = button.TextBox 
local remoteEvent = game.ReplicatedStorage.Remotes:WaitForChild("LoadMap")
local remoteEvent2 = game.ReplicatedStorage.Remotes:WaitForChild("MapData")
local NotificationHandler = require(game.ReplicatedStorage.NotificationHandler)

button.MouseButton1Click:Connect(function()
	
	NotificationHandler:CreateNotification("default", "Loading Map please wait")
	warn("Map loader process began")
	local modelIdString = textLabel.Text
	local modelId = tonumber(modelIdString)

	if modelId then
		remoteEvent:FireServer(modelId)
	else
		NotificationHandler:CreateNotification("alert", "Invalid Model ID:"..modelIdString)
		warn("Invalid Model ID in TextLabel:", modelIdString)
	end
end)

remoteEvent2.OnClientEvent:Connect(function(MapName)
	NotificationHandler:CreateNotification("success", "Successfully loaded "..MapName)
end)

The server side of the Map Insertion

local remoteEvent1 = game.ReplicatedStorage.Remotes:WaitForChild("LoadMap")
local remoteEvent2 = game.ReplicatedStorage.Remotes:WaitForChild("ClearMaps")
local remoteEvent3 = game.ReplicatedStorage.Remotes:WaitForChild("MapData")
local Maps = game.ReplicatedStorage.Maps
local InsertService = game:GetService("InsertService")


local InsertService = game:GetService("InsertService")

remoteEvent1.OnServerEvent:Connect(function(player, modelId)

	print(player.Name .. " sent Model ID: " .. modelId)

	local success, model = pcall(function()
		return InsertService:LoadAsset(modelId)
	end)
	
	if success and model then
		model.Parent = Maps
		wait(1)
		local Map = model:GetChildren()[1]
		Map.Parent = Maps
		model:Destroy()
		local MapName = Map:WaitForChild("MapSettings"):WaitForChild("MapDetails"):WaitForChild("MapName").Value
		print("Model with ID " .. modelId .. " loaded successfully by " .. player.Name)
		remoteEvent3:FireAllClients(MapName)

	else
		warn("Failed to load model with ID " .. modelId .. ":", model)
	end
end)

Part of the round system responsible for loading the map

	local random = math.random(1, #maps)

		local map = maps[random]:Clone()
		map.Name = "MapInsert"
		
		local Name = map:WaitForChild("MapSettings"):WaitForChild("MapDetails"):WaitForChild("MapName")
		status.Value = "Map chosen: "..Name.Value
		
		wait(2)
		
		status.Value = "Loading Map, please wait"

The game gets hung up on
local Name = map:WaitForChild(“MapSettings”):WaitForChild(“MapDetails”):WaitForChild(“MapName”)
Because it doesn’t exist in the server’s view, even when it’s imported from a server-side script
Anything I should try?

It would probably be more helpful if I included the entire part of the map loading process

while true do
	if forcestart then
		for i = 1,10 do -- Intermission time
			status.Value = "Match starts in.. "..10-i -- Intermission time
			wait(1)
		end

		local random = math.random(1, #maps)

		local map = maps[random]:Clone()
		map.Name = "MapInsert"
		
		local Name = map:WaitForChild("MapSettings"):WaitForChild("MapDetails"):WaitForChild("MapName")
		status.Value = "Map chosen: "..Name.Value
		
		wait(2)
		
		status.Value = "Loading Map, please wait"
		
		local time = map:WaitForChild("MapSettings"):WaitForChild("Lighting"):WaitForChild("ClockTime")

		local MapCreator = map:WaitForChild("MapSettings"):WaitForChild("MapDetails"):WaitForChild("Creators").Value
		local MapImage = map:WaitForChild("MapSettings"):WaitForChild("MapDetails"):WaitForChild("MapImage").Value
		local Name = map:WaitForChild("MapSettings"):WaitForChild("MapDetails"):WaitForChild("MapName").Value
		local MapWater = map:WaitForChild("Water")
		local GameWater = game.Workspace.WaterInGame
		local OriginalLighting = game.Lighting
		local LightingContents = map:WaitForChild("MapSettings"):WaitForChild("Lighting"):WaitForChild("LightingContents") --Model you want to destroy
		remoteEvent2:FireAllClients(MapImage, MapCreator, Name)
		
		for k, v  in  map:GetChildren()  do 

			v:Clone().Parent = workspace.Map

			--A small delay so that client and server can catch up and reduce lag.
			task.wait(0.1)

		end

send a ss of the map’s children in explorer (fully open up map settings)

This is what the Model looks like fully opened. That empty model above is where the map is unpacked for all the game mechanics to work.

When I import the map from the game Console then run the Round manager, it shows the script cannot see the model at all

The map is Technically being inserted into the game correctly because it arrives in the dedicated folder in Replicated storage (Shown in the image below)

But now I have a new issue, the game thinks that the folder is empty, even though there’s a model in the folder right there


Any updates, i feel like this should just work like normal

This is way too dangerous. If the server takes a ModelId from the client and loads it with InsertService, a hacker can just send a malicious ID. Then, all the scripts inside that model will run on the server. You should never trust the client.

2 Likes

Oh ok, so how should i get the Model id of the map to the Server script from the button without sending the ID through a remote event?


Oh i didnt see that

I think it would be better to turn off all scripts on the inserted model.

I’m pretty much guessing here, but could the round system be starting too early? Like before the map has been fully loaded/inserted?

If the map contents both exist on the server and the client, then another suspicion I have is maybe the variable is pointing to the wrong map model (or the variable gets updated to something else somewhere in the script).

1 Like

I’m going to get back to looking into the issue, you might be right on the second suspicion

Alright, so here are a few developments on the issue:

Ive put debugs that print out the names of the children in the map folder in all of the scripts that are related to the Map Loader (Round manager and the model Inserting script)

1 issue is that the ScrollingGrid GUI is still warning me about a yield possible yield in the MapSettings folder, even though it finds it anyway, because that’s where it grabs the map’s name and Thumbnail from.

The second issue I have now is that I’m getting this random error

I don’t know if this is a confirmation that the server is finally finding models inside the Folder now, or just another wall I have to wrap around, I’ve never seen this before

???

The Math.Random is still not finding anything in the folder, even though the same script printed out the children, not even a few seconds ago

Ive renamed the post so the issue is more straightforward than it used to be, but the issue is still here. Ive tried multiple ideas in the past for days, but haven’t gotten much luck.

Im sure I’ve scripted this correctly, all of my ModelStreaming settings are correct, and im importing the model from a server script triggered by a Remote Event like I should be. Is there anything im missing?

The first issue is that your script is Yielding from the :WaitForChild(). Please add a maximum interval that the script can wait for.

Next can you be specific and show us where line 43 of the round handler is, and also in your script show us where your variable maps is defined?

Alright so I figured it out, apparently Math.random and Random.new just suck.

So I first tried to see if the Remote Event was the issue, a made a little button in the lobby that uses InsertService to load in a pre-set Model


But the Random.New function was still giving me the blues, so I just decided to Jerry-rig the random map process by naming every inserted Model to just “Model”, that way each map appears the same and its a random selection. this eliminates the need for using Math.random or Random.new


I thought that the issue was rooting from the Local script triggering everything, but it was just 1 faulty line of code, woops…

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.