I have a map system in my game where a map is loaded into a model called "Map" inside workspace. My maps are named Map1, Map2, Map3, etc. I need to get the actual map inside the map object so that I can clone something that's inside of that. How can I write code that works for all of my maps, no matter what the name is? Thanks in advance.
How do I get an object inside a model by only knowing the first part of its name?
1 answer
This seemed fun so I thought I'd answer it.
- I recommend you use the server storage for each of your maps.
- I recommend you use a folder called Maps
- I recommend your maps be grouped into a model.
- I recommend you clone the map and set its parent to the workspace to spawn it. It will spawn directly where you built it.
This script will be made with the above in mind.
local storage = game:GetService("ReplicatedStorage")
function SpawnMap(MapNum)
for _, Maps in pairs(storage:GetChildren()) do
local MapToSpawn = "Map" .. MapNum
if Maps.Name == MapToSpawn then
Maps:Clone().Parent = workspace
local MapObjects = Maps:GetChildren()
for _, MapObject in pairs(MapObjects) do
if MapObject.Name == "PlayerStartDoor" then
local PlayerStartDoor = MapObject
PlayerStartDoor.Lock = true -- Door is now locked
end
end
end
end
end
This scripts spawn a map of your choosing.
SpawnMap(1)
This will spawn Map1
when Map1 spawns the script will look for the door and lock it.
Loops are fun.
2 comments
Thanks, but I already have the map selection system (similar to yours) completed. What I'm really asking is if there's a a way to reference the current map in another script just by the fact that it's first few letters are identical to every other map (e.g., Map1, Map2, Map3 all have "Map" as their first few letters).
I believe I answered your question within my script. By using the variable local MapToSpawn = "Map" .. MapNum Where since the name of your Maps always begin with Map all you need to do is check the number behind it.
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
I haven't really found out how to accomplish this, but I think I figured out a solution to this. It's been a while since I've coded anything in ROBLOX, so it's just taking me some getting used to. The :GetChildren() function should work fine here, and I'd just reference the first (and only, in my case) value since the only object contained in the "Map" model is the map that is currently being play