Invalid argument #2 (string expected, got table)

hello, my friend has been encountering a problem with a script he has been working on.

Here is the script.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptStorage = game:GetService("ServerScriptService")

return function (context, text, number)
    local Enemy = require(ServerScriptStorage.Main.Mob)
    local EnemyFolder = ReplicatedStorage:FindFirstChild("Mobs")
    local map = workspace.Map
    
    local findfirstChild = function(inst, str:string)
        if typeof(str) ~= "string" or typeof(inst) ~= "Instance" then return end

        str = str:lower()
        for Index, Object in ipairs(inst:GetChildren()) do
            local ObjectName = Object.Name:lower()
            if ObjectName == str or string.find(ObjectName, str) then return Object end
        end
    end
    
    local childFound = findfirstChild(EnemyFolder, text)
    
    Enemy:Spawn(childFound.Name, number, map)
end

he has been encountering

invalid argument #2 (string expected, got table)

on

if ObjectName == str or string.find(ObjectName, str) then return Object end

both ObjectName and str are strings and are not tables, my mind has been melted from trying to fix this. what seems to be the problem?

Instead of making the object’s name a variable, try just doing this

for Index, Object in ipairs(inst:GetChildren()) do
            if ObjectName == str or string.find(Object.Name:lower(), str) then return Object end
        end

this does not help with my problem, this was done in an earlier version I just wanted to not repeat the same thing 2 times.

Can you provide the script which uses this function?

1 Like