Why is my Wifi Widget Crashing?

Hi there - sorry if this has been asked before but I've tried searching the forum with no joy!

I'm trying to make an AppleScript Widget thats turns Wifi on and off, and updates the icon to match the whether the Wifi is on or off. I'm fairly new to AppleScript and BTT and am trying to understand why my Widget seems to crash (it disappears from the touch bar and doesn't return unless I restart BTT or change a setting and 'Save').

Here is the script I'm using...

tell application "System Events"
	set wifi_status to do shell script "networksetup -getairportpower en0"
	if wifi_status = "Wi-Fi Power (en0): On" then
		do shell script "networksetup -setairportpower en0 off"
		tell application "BetterTouchTool"
			update_touch_bar_widget "C245EEE0-E407-4AE0-A34B-325D8A4EC207" text "WifiOff" icon_path "/PATH/TO/ICON" background_color "255,100,100,255"
		end tell
		do shell script "networksetup -setairportpower en0 on"
		tell application "BetterTouchTool"
			update_touch_bar_widget "C245EEE0-E407-4AE0-A34B-325D8A4EC207" text "WifiOn" icon_path "/PATH/TO/ICON" background_color "100,100,100,100"
		end tell
	end if
end tell

It works exactly how I'd hope it to if I run it within macOS's Script Editor or BTT's built in script editor, but for some reason it crashes when BTT is running as it would normally. Conveniently it crashes after the amount of time set in "Execute script every..." so seems to be connected to that? No other widgets/icons crash or disappear, just the Wifi one I'm working on.

Sorry if this is too much information, just really hoping to sort with out!


On the off chance this helps someone in the future, I manged to get it working. Instead of running the applescript in the 'Widget Specific' Apple Script section, it seems happier running as an Assigned action to Run Apple Script (async in background) - just leaing the Widget Specific Apple Script empty.

Also setting it to Execute every 0 seconds has helped, didn't even occur to me that that might work but definitely has done something!