Code leading to duplicator crash

I can’t figure out why my code makes the Garry’s duplicator crash.
To replicate this I have to copy my entity, undo the old one and paste the dupe. Then it crashes.
The advanced duplicator works without any errors and the fact that I can’t see any potential errors before the crash makes this even more harder for me.

This is the code that’s causing the regular duplicator to shit itself, please note I’ve never worked with constraints via lua so feel free to tell me what I’m doing wrong.




function ENT:SetupWheels(tank)
     local wheelspinvec = Vector(1,0,0)
     local selfpos = tank:GetPos()
     
     local wh0 = ents.Create("gb_g_goliath_wheel")
     wh0:SetPos(selfpos + Vector(13.75,18,-5))
     wh0:SetAngles(Angle(0,0,0))
     wh0:Spawn()
     wh0:Activate()
     
     local wh1 = ents.Create("gb_g_goliath_wheel")
     wh1:SetPos(selfpos + Vector(-14,18,-5))
     wh1:SetAngles(Angle(0,180,0))
     wh1:Spawn()
     wh1:Activate()
     
     local wh2 = ents.Create("gb_g_goliath_wheel")
     wh2:SetPos(selfpos + Vector(-14,0,-5))
     wh2:SetAngles(Angle(0,180,0))
     wh2:Spawn()
     wh2:Activate()
     
     local wh3 = ents.Create("gb_g_goliath_wheel")
     wh3:SetPos(selfpos + Vector(13.75,0,-5))
     wh3:SetAngles(Angle(0,0,0))
     wh3:Spawn()
     wh3:Activate()
     
     local wh4 = ents.Create("gb_g_goliath_wheel")
     wh4:SetPos(selfpos + Vector(-14,-18,-5))
     wh4:SetAngles(Angle(0,180,0))
     wh4:Spawn()
     wh4:Activate()
     
     local wh5 = ents.Create("gb_g_goliath_wheel")
     wh5:SetPos(selfpos + Vector(13.75,-18,-5))
     wh5:SetAngles(Angle(0,0,0))
     wh5:Spawn()
     wh5:Activate()
     
     if self:IsValid() and self then
     constraint.Axis(wh0,tank,0,0,Vector(0,0,0),selfpos,0,0,0,1,wheelspinvec)
     constraint.Axis(wh1,tank,0,0,Vector(0,0,0),selfpos,0,0,0,1,wheelspinvec)
     constraint.Axis(wh2,tank,0,0,Vector(0,0,0),selfpos,0,0,0,1,wheelspinvec)
     constraint.Axis(wh3,tank,0,0,Vector(0,0,0),selfpos,0,0,0,1,wheelspinvec)
     constraint.Axis(wh4,tank,0,0,Vector(0,0,0),selfpos,0,0,0,1,wheelspinvec)
     constraint.Axis(wh5,tank,0,0,Vector(0,0,0),selfpos,0,0,0,1,wheelspinvec)
     end
end

This function is called in ENT:SpawnFunction, so the wheels are only added when you actually spawn the entity to prevent duplicator adding more sets of wheels.
I just want to know if the crash is because of my code, or is it something with the duplicator itself.