This is regarding Minecraft: Bedrock Edition. I am trying to automatically change players to adventure mode when when entering a town and back to survival mode when leaving. I created a function which is executing in a repeating command block. The below commands basically create 2 towns. If player 1 goes into player 2 town it puts them in adventure mode. When player 1 enters their own town it puts them in creative mode. Leaving both towns will put them in survival mode.
#run this one time to create a townMode variable
#scoreboard objectives add townMode dummy
scoreboard players set @a[scores={townMode=!0}] townMode 0
#town for player 1
scoreboard players set @a[name=player1name, scores={townMode=!1}, x=501, y=62, z=151, dx=49, dy=49, dz=49] townMode 1
scoreboard players set @a[name=!player1name, scores={townMode=!2}, x=501, y=62, z=151, dx=49, dy=49, dz=49] townMode 2
#town for player 2
scoreboard players set @a[name=player2name, scores={townMode=!1}, x=501, y=62, z=93, dx=49, dy=49, dz=49] townMode 1
scoreboard players set @a[name=!player2name, scores={townMode=!2}, x=501, y=62, z=93, dx=49, dy=49, dz=49] townMode 2
gamemode 0 @a[scores={townMode=0},m=!0]
gamemode 1 @a[scores={townMode=1},m=!1]
gamemode 2 @a[scores={townMode=2},m=!2]
Its basically working BUT I noticed that the command block stops executing the commands if I am too far away. If I put the command block in the center of the town it works but if I put it really far away it doesn't seem to trigger consistently. I noticed this after a few hours beating my head against the table. I saw via commandblockoutput the commands stop running if I'm too far from the command block. Is there any workaround where I can have the command block far from the villages but still work?