Sorry for the delayed response. I found out what is happening.
I have 2 scripts, namely: afterlogin.sh and afterlogout.sh.
afterlogin contains:
#!/bin/bash
gsettings set org.mate.background picture-filename ”
The problem is that it is run under user nx instead of the logged user. So it cannot get the DBUS_SESSION_BUS_ADDRESS variable from the current gnome session to change the background.
Is there a way to run scripts as the current logged user, instead of user nx?