Script working on an object but not on another
Custom properties not set
The driver script as is will throw an error if the custom properties &quot;z&quot; and &quot;prev_z&quot; are not set on the object.
Look for errors in the console.
Where does console output go
Add the appropriate custom properties to the object being driven for it to function as expected.
Note.
The object referred to as self in a driver is an  evaluated copy of the object.
Silly test script to ensure the original object has a custom property &quot;foo&quot; set from within the driver.
Returns the evaluated value of the custom property, if it exists, else 0.0.
from bpy.app import driver_namespace as dns

def test(ob):
    ob.original[&quot;foo&quot;] = ob.original.get(&quot;foo&quot;, 0.0)
    return ob.get(&quot;foo&quot;, 0.0)

dns[&quot;test&quot;] = test

copy and paste this driver into any object to create a &quot;foo&quot; custom property if one does not already exist.