First, don't use "my" when defining variables outside of a sub event. Second, if you define your variable outside of the sub like you did for checking door state, it won't update ever again after the script is initially loaded. This means it won't know if the door state has changed. If you use the script below, that will at least resolve those 2 issues:
Code:
sub EVENT_CLICKDOOR
{
my $doorstate = quest::isdooropen(40);
if($doorid == 41 && !$doorstate)
{
quest::forcedooropen(38);
quest::forcedooropen(39);
quest::forcedooropen(40);
}
if($doorid == 41 && $doorstate)
{
quest::forcedoorclose(38);
quest::forcedoorclose(39);
quest::forcedoorclose(40);
}
}