I think your problem is here:
if collision != null and collision.collider.is_in_group("Player"):
intersect_ray() will return an EMPTY dictionary if no ray intersection is found. So, I assume that's what's happening here. However, an EMPTY dictionary IS NOT null, so it'll get through that first
!= null check without issue. Then, the very next check attempts to access the
collider key, which won't exist and triggers the error you report.
Change the above code to:
if collision and collision.collider.is_in_group("Player"):