Pass a variable (folder pathway) to AppleScript in Automator
When using on run {input, parameters}, input is a list, and as such you need to change the following line of your code:

set thefolder to (input as text)


To:

set thefolder to (item 1 of input)


That will fix the Can’t get every file of ... error.

Also, while you didn't state the complete settings of the Get Value of Variable action, I'll assume that under Options, you have checked the [√] Ignore this action's input checkbox to unlink it from the preceding Set Value of Variable action. Otherwise, it passes the variable twice to the Run AppleScript action! That is, in this example workflow you've presented.