Invalid argument #2 (string expected, got Instance)

Hello, so i made this module function, which essentially is used for loading maps in the game, however it gives the error in the title at the line “Root.CFrame = Map.FLDR*Spawns[Location].CFrame” , the script is a module script located in a folder in ReplicatedStorage

function Functions:LoadMap(Map, Ambience, Location)
    for , Obj in ipairs(Maps_Folder:GetDescendants()) do
        Obj.Parent = LoadMaps_Folder
        Map.Parent = Maps_Folder
        Root.CFrame = Map.FLDR_Spawns[Location].CFrame
        Ambience.Playing = true
        local function Map_Name(Name_UI)
            local TWMN_Info = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
            local Tween_MapName1 = Tween_Service:Create(Name_UI, TWMN_Info, {TextTransparency = 0})
            local Tween_MapName2 = Tween_Service:Create(Name_UI, TWMN_Info, {TextTransparency = 1})
            Name_UI.Visible = true
            Tween_MapName1:Play()
            task.wait(3.5)
            Tween_MapName2:Play()
            task.wait(0.75)
            Name_UI.Visible = false
        end
        if string.match(Map.Name, "Crossroads") then
            Map_Name(MapNames_Folder.Name_Crossroads)
        elseif string.match(Map.Name, "RobloxHQ") then
            Map_Name(MapNames_Folder.Name_RobloxHQ)
        end
    end
end

Can you tell me on which line the problem occurred?

(I found it)

Oh, well you can try this:

Root.CFrame = Map.FLDR_Spawns:FindFirstChild(tostring(Location)).CFrame

If this doesn’t work then reply to this message and I’ll look in deeper.

Invalid argument #2 (type expected, got type) usually occurs when you use invalid types on an operation.

in the suspected line:

 Root.CFrame = Map.FLDR_Spawns[Location].CFrame

you are using indexing operators [] on a folder.
In luau, indexing using instances is only allowed on tables as a key value pair.

when using indexing on an instance (folder) you need to pass a string.
so if you could reveal what location is, then I can help you.
But currently all I can suggest is adding .Name to location if it is a model.

like so;

 Root.CFrame = Map.FLDR_Spawns[Location.Name].CFrame

if you could reveal the types of the arguments and their purpose that would help in identifying the issue.

the other person suggested tostring(), but if location is a model or anything else, like a table, that won’t work.
for instances, use the .Name property instead.

hope this helps a bit.

1 Like

Sadly that didn’t work at all :frowning:

Thanks, i replaced it with Root.CFrame = Map.FLDR_Spawns[Location.Name].CFrame and it actually worked!

1 Like

this is not the right section for script help

1 Like

You should mark @scavengingbot’s answer as a solution if it helped you.

What the heck i swear to God i put the post in Scripting Support!

The solution icon doesn’t seem to appear, at least not in this category.