Cannot get Lua Scripts to work

I am currently trying to get a couple of Lua Scripts running on a drone with a Pixhawk 4 FC and followed all of the steps that are necessary (Enabling scripts by setting SCR_ENABLE to 1, uploading scripts through MAVLink on MP and directly uploading the file to the SD card, setting the drone to auto mode while it is already in the air, ensuring that the scripts don’t have errors (I’ve mainly been using the hello world file found on the sample script github page), and ensuring that the heap size is adequate for the scripts I’m using. I’m admittedly quite inexperienced with this so I could be missing something stupid, but if anyone has any ideas of what steps to take from here, it would be highly appreciated.

What are your script files named? Where are they on the SD card?

1: HelloWorld.lua, LuaTest1.lua, Test1.lua, Test2.lua, Test3.lua, and Test4.lua
2: They’re in the scripts folder on the SD card.
Also sorry for the delayed response, I’ll try to be quicker when responding in the future.
If you’d like I could also post the scripts I used.

Exactly like this?

Yes, exactly like that.

Then attach the scripts. It’s possible they’re working and you’re just missing the output.

HelloWorld.lua:
– This script is an example of saying hello

gcs:send_text(0, “hello, world”) – send the traditional message


LuaTest1.lua:
[[ This program is a test program for utilizing lua scripts to fly a drone.
Should this program fail, the program was either written incorrectly, or the drone isn’t able
to work with Lua]]

[[Getting Starting Coordinates of Drone]]

startLat = lat()
startLng = lng()
startAlt = alt()

[[coordinates Received]]

function RTS() [[RTS stands for RETURN TO START]]
lat(startLat)
lng(startLng)
alt(startAlt)
end

[[Moving the Drone up & down]]

alt(startAlt + 10)

alt(startAlt + 5)

alt(startAlt + 3)

[[Altitude Test Complete]]

[[Moving Left, Right, Forward, and Backward]]

lat(startLat + 10)
lng(startLng + 6)
lat(StartLat)
lng(startLng)
lat(startLat - 10)
lng(startLng - 6)
lat(StartLat)
lng(startLng)

[[Direction test completed]]

[[Landing]]

RTS()


Test1.lua
local file = io.open( "PrintTextHere.txt , a )
file:write( “Test 1/n”)
gcs:send_text(0, “Test 1”)

Test 2, 3 and 4 are the same at test 1 except the number is replaced.

I suspect that your hello world script is running, but you’re probably not connecting a GCS fast enough to catch the output.

I don’t know where you got the position test script, but it references no bindings and will never work as written (nor would a similarly structured script work, even if the bindings were correctly referenced).

File IO appears to be attempting to save to the root of the SD card, but there are multiple missing quotation marks.

That makes sense, I was a little bit weirded out about how my script (LuaTest1.lua) looked very bare bones. Also I didn’t catch that there were quotes missing, I’ll fix that and update you on what happens. Thank you!

Suggest you get one script working rather than dumping a whole slew of them onto the SD card.

Sounds good, also, does the drone need to be in any specific mode when it is running scripts?

Scripts begin running at boot time. No mode required.