How to convert string to a model

I need help, I have been stuck with this problem for the past hours
I have 0 clue how to convert a string to a model.
Here’s my script.

game.Players.PlayerAdded:Connect(function(player) --gets player
			player.Chatted:connect(function(chatmsg)				
				if chatmsg:sub(1,4) == "map/" then --reads if user said map/
						local map = chatmsg:sub(5, chatmsg:len()) --reads what map user wants
					if player:GetRankInGroup(GroupId) >= min and player:GetRankInGroup(GroupId) <= max then --checks group ID and rank
						print("user typed")
						current_map:ClearAllChildren()--clears current map
						print("old map cleared")
						if game.ServerStorage:FindFirstChild(map) then -- checks if the current map exists
							print(map) -- prints what map is being generated
							h.text
							map = game.ServerStorage[map] -- now we're actually defined map as the model top load in
							for k, v in ipairs(map:GetChildren()) do -- map is a string, when you call :GetChildren() it will flag an error.
								v:Clone().Parent = current_map --clones the map
								wait() --gives the server time to handle other operations instead.
							end

	
						
						print("Map loaded.")

					end
				end
			end
		end)
	end)

The script is supposed to load a map when the user types map/(map name). But the problem is, when at for k, v in pairs(map:GetChildren()) do --loop (broken for some reason) line I get this error.
image

Any help would be appreciated.

Edit: Issue was solved and changed the old code, feel free to use my map loader.

Your issue is this part right here, I have amended the commenting for you by the way:

if game.ServerStorage:FindFirstChild(map) then -- checks if the current map exists
	print(map)
	for k, v in pairs(map:GetChildren()) do -- map is a string, when you call :GetChildren() it will flag an error.
		v:Clone().Parent = current_map --clones the map
		wait() --gives the server time to handle other operations instead.
	end
end

What you need to do instead is say:

if game.ServerStorage:FindFirstChild(map) then -- checks if the current map exists
	print(map) -- this is a string
	map = game.ServerStorage[map] -- now we're actually defined map as the model top load in
	for k, v in pairs(map:GetChildren()) do -- map is a string, when you call :GetChildren() it will flag an error.
		v:Clone().Parent = current_map --clones the map
		wait() --gives the server time to handle other operations instead.
	end
end

This way we define map as an actual model instead of a string, :FindFirstChild() only returns the child if you actually say:

map = game.ServerStorage:FindFirstChild(map)

Else all your going when you place it in an IF statement is checking if it actually exists and not storing it.

3 Likes

map is a string, you called getchildren on a string.
6Clu’s reply should fix the issue

1 Like

Thank you, really, thank you a lot.
I was at first confused why my script didn’t work, because my old loading method was to move the map from lighting to workspace, but due to having implemented big maps roblox crashed. So I added a loop to make it load in part by part.
Again, thank you a lot!

Is it necessary to add a couple 10 000 ms to the operation?

Can’t we use ipairs to not loop in key - value pairs?

@OmgItsItalian

Yes we can and it is is much faster, recommended too unless you’re going to need the index associated, which does not seem to be the case

1 Like

Hmm, I was not that sure what the difference from ipairs and pairs was until now, are there any other uses for ipairs?

there is a difference,

Ipairs is currently the fastest generator and can iterate through index-value pairs which is faster than iteration in key-value pairs.

pairs uses the next iterator function so returns nil being nil encountered in a contiguous array, ipairs however is different as it will stop the iteration rather than returning nil. Pairs has also has no specific order for non-numerical indices, it iterates over all indices , while ipairs doesn’t index non-numerical indexes in the first place.

If it is just a matter of iterating through a simple array, there will be no difference in the output of ipairs and pairs though there will be a difference in speeds.

It is recommended that you use pairs when you want to index all elements.

Example :

dictionary = { 
 
["things"] = 1;
["xyz"]    = "a";
["name"]   = "k"
              
             }

 
 for k, v in pairs (dictionary) do
          print(k..v)
  end

Output :

image

however you cannot index non-list items with ipairs, so doing the same with ipairs instead will give this output :

image

Nothing.

Note that ipairs is built to work with arrays with numerical indices, pairs is designed to work with dictionaries.

Here is a small experiment I did to find the average time taken by different means of iteration to execute code:

DO NOT use pairs incorrectly, please

2 Likes

To me, its a method of doing it - I am not particularly bothered about relatively small time differences. I also work heavily with dictionaries and not lists by themselves, hence to me its just preference to consistently use pairs instead of ipairs.

In his scenario their will be miniscule difference between pairs and ipairs - these iterator functions are only considered when your trying to search through thousands of items within the array itself.

You’ve done a nice test however complainably you could obviously say:

  • the print method could have an effect,
  • the general servers could have different pressures / loads at different times.
  • only done the experiment once? would it not be a better reflection if you did it 5 times?

This isn’t me saying ipairs isn’t faster (because it is), but what I am saying is your values won’t reflect the exact difference. I would however be interested in properly measuring the difference - but that would mean I would need to be close to a server box and with super stable internet / processing power.

epic point out though :+1:

1 Like