Search code examples
robloxluau

Can't run function when trying to find ClickDetector


if I were to run correctbook.MouseClick:Connect(onTouch) it will work however when i try to do it with the array it doesn't run it, anything i'm missing or is it just not possible ?

`local bookScript = script.Parent

local books = game.Workspace.reading.books

local correctBook = game.Workspace.correctBook.ClickDetector

local bookArray = {
    
    books.book1,
    books.book2,
    books.book3,
    books.book4,
    books.book5,
    books.book6,
    books.book7,
    books.book8

}

local allBooks = bookArray.ClickDetector



function onTouch(allBooks)
    
    
    for everything = 1, #bookArray do
        
        print("clicked")        
        
        
    end
    
end

allBooks.MouseClick:connect(onTouch) -- whole line doesn't work--`

I already tried adding clickdetector in the array but it still doesn't work


Solution

  • Missing variables got moved to another script and used a what I'm guessing is a different kind of for loop

    local books = game.Workspace.reading.books
    
    local correctBook = game.Workspace.correctBook.ClickDetector
    
    local bookArray = {
        books.book1.ClickDetector,
        books.book2.ClickDetector,
        books.book3.ClickDetector,
        books.book4.ClickDetector,
        books.book5.ClickDetector,
        books.book6.ClickDetector,
        books.book7.ClickDetector,
        books.book8.ClickDetector
    }
    
    function onClick()
        print("Clicked")
    end
        
    
    for everything, allBooks in ipairs(bookArray) do
        allBooks.MouseClick:Connect(onClick)
    end