BOTH naughty and nice, "Galaxy Quest" manages to poke wicked fun at "Star Trek" and its devotees while at the same time serving up sincere homage to the beloved sci-fi serial and its many spinoffs. What writers David Howard and Robert Gordon get exactly right is the revelation of every Trekkie's dirty secret: the fervent hope (faith, really) that somewhere beyond the final frontier there really are such things as Denebian Slime Devils and alien chicks with bodacious cleavage.

The premise: A band of extraterrestrials has intercepted reruns of the now canceled and suspiciously "Trek"-like "Galaxy Quest." Mistaking episodes of old TV shows for "historical documents," the Thermians (a naive and pacifist race beset by the savage army of Roth'h'ar Sarris of Fatu-Krey) track down the cast signing autographs for pencil-necked geeks at a fan convention, where they recruit the has-beens to lead them in battle aboard a working reproduction of their fictional spaceship.

Where Howard, Gordon and director Dean Parisot stumble is in taking almost 40 minutes to get this simple exposition out of the way. Everyone's waiting for the real fun to begin and that can't happen until the clueless crew (Tim Allen, Sigourney Weaver, Alan Rickman, Tony Shalhoub, Daryl Mitchell and Sam Rockwell, whose character played doomed Crewman No. 6 on episode 81) get transported into a world filled with high-tech controls they don't know how to operate, dangerous equipment that hasn't been tested on human beings and an enemy so ruthless they make Klingons look like Quakers.

But once Allen, whose character played Commander Peter Quincy Taggart on the old series, settles into the fine Corinthian leather swivel chair of the Protector's command deck (in a posture reminiscent of Captain Kirk's famous slouch), "Quest" kicks into warp drive. The wisecracks fly fast and furious: jokes about Taggart's propensity to doff his shirt during hand-to-hand combat and to become romantically involved with any life form in a skirt bump up against sight gags based on the TV show's frequent habit of forcing one of the crew to crawl through the ship's elaborate and treacherous ductwork.

When Taggart -- stranded on a barren planet and forced to fight, gladiator-style, a creature made entirely of rock -- radios the ship for advice, Crewman No. 6 offers a piece of advice that will sound strangely familiar to followers of "Star Trek":

"Can you fashion some sort of rudimentary lathe?"

You kind of have to love "Trek," with all its absurdities, to really get "Quest" -- and if you don't love it, what kind of a human are you anyway? The greatest thing about this movie is that when the Thermians finally have their backs against the wall and all hope seems lost, it isn't Taggart and company who save the day after all, but an amusing deus ex machina whose surprise arrival makes clear exactly why Hollywood will be able to keep making movies like the upcoming "Star Trek 3D" well into the next millennium.

GALAXY QUEST (PG, 102 minutes) -- Contains science fiction goo and "Star Trek"-style combat. Opens Saturday at area theaters.