How to keep an object inside a spherical collision?

I went searching about how to (this posts title), I saw a thread about doing that in Unity. I recreated the code from this forum in Defold:

local radius = 31
local position = message.other_position
local distance = vmath.length(position - go.get_position())

if distance >= radius then
  local origin = go.get_position() - position
  origin = origin * radius / distance
  go.set_position(position + origin)
end

theres 2 problems:

i dont have access to the sphere radius that belongs to the collisionobject. I cant use the number i set in the editor, idk why it just is not working. Instead, i debugged until i found out that the radius is about 31 in in-game units.

The second problem cannot be overcomed: the code works, but if the object is pushed way far out of the sphere bounds, the collision will fail. For some reason, pushing the object away from the sphere still makes it move, until it snaps out of the sphere.