Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I get an object inside a model by only knowing the first part of its name?

Asked by
Klamman 220 Moderation Voter
7 years ago

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.

0
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 Klamman 220 — 7y

1 answer

Log in to vote
-1
Answered by 7 years ago
Edited 7 years ago

This seemed fun so I thought I'd answer it.

  1. I recommend you use the server storage for each of your maps.
  2. I recommend you use a folder called Maps
  3. I recommend your maps be grouped into a model.
  4. 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.

0
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). Klamman 220 — 7y
0
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. User#5748 20 — 7y
Ad

Answer this question